java裡的日期格式 UTC和本地時間互轉
阿新 • • 發佈:2018-12-04
Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) -1);//當前時間的前一小時舉例 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); String end=dateFormat.format(new Date());//將本地日期格式化為UTC格式的 日期字串
================================================
//字串轉Date
String stringDate = "Thu Oct 16 07:13:48 GMT 2015";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM ddHH:mm:ss 'GMT' yyyy",Locale.US);
Date date =sdf.parse(stringDate);
System.out.println(date.toString());
//Date轉字串
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
=====================================================
//T代表後面跟著時間,Z代表UTC統一時間
//格式化時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String time = sdf.format(new Date());
System.out.println(time);
//解析時間 2016-01-05T15:09:54Z
Date date = sdf.parse(time);
System.out.println(date);