UTC(世界协调时间也就是格林尼治时间,和北京时间差了八个小时)时间格式 ISO 8601 日期-时间格式表示:yyyy-mm-ddThh:mm:ss.fffZ,其中 yyyy = 年,mm = 月,dd = 天,T 表示时间部分的开头,hh = 小时,mm = 分钟,ss = 秒,fff = 几分之几秒,而 Z 表示祖鲁语(另一种 UTC 表示方法)
//当前本地时间Date 对应的 UTC时间String
Date date = new Date();
System.out.println("本地时间Date: " + date);
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz); //获取时区
String nowAsISO = df.format(date);
System.out.println("本地时间Date--->UTC时间String:" + nowAsISO);
//UTC时间String 对应的 本地时间Date
String utcString = "2019-08-29T10:13:24.725Z";
System.out.println("UTC时间字符串:" + utcString);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); //获取时区
Date isoStringToDate = null;
try {
isoStringToDate = sdf.parse(utcString);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("UTC时间String--->本地时间Date: " + isoStringToDate);
2 Date
//当天开始日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
Date dayBegin = calendar.getTime();
//当天结束日期
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,23);
calendar.set(Calendar.MINUTE,59);
calendar.set(Calendar.SECOND,59);
calendar.set(Calendar.MILLISECOND,999);
Date dayEnd = calendar.getTime();
//当月开始日期
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date monthBegin = calendar.getTime();
//当月结束日期
calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
calendar.set(Calendar.HOUR_OF_DAY,23);
calendar.set(Calendar.SECOND,59);
calendar.set(Calendar.MILLISECOND,999);
Date monthEnd = calendar.getTime();