GMT:GreenwichMeanTime,格林威治标准时间//例1:根据1970-1-1到某个时间点经过的毫秒数,引入具体时间点//1.创建一个Calendar对象,设置object'svalueaccordingtothemillisecondsTimeCalendarc=Calendar.getInstance();c.setTimeInMillis(46800999);//2.创建一个SimpleDateFormat对象,指定格式hour:minute:secondDateFormatdf=newSimpleDateFormat("HH:mm:ss");//3.设置时区,GMT表示0时区df.setTimeZone(TimeZone.getTimeZone("GMT"));//4.格式化时间System.out.println(df.format(c.getTime()));//例2:根据某个字符串获取指定格式的日期Stringstr="2011-01-1800:00:00.0";DateFormatdf=newSimpleDateFormat("yyyy-MM-dd");Datedate=df.parse(str);Stringnewstr=df.format(date);//获取所有可用时区String[]zones=TimeZone.getAvailableIDs();//模式字母yyearM年中月日月中日日年中日H日中时(0-23)h日中时(1-12)m时中分时分中秒S毫秒注意!创建Calendar类对象并设置时区后,Calendar.getTime()方法返回一个java.util.Date对象。这个Date对象没有使用Calendar对象的时区,而是当前系统的时区;SimpleDateFormat和DateTimeFormatter的区别在于创建对象的方式:SimpleDateFormat通过new创建对象,是线程不安全的,所以在多线程程序中,每次使用SimpleDateFormat都要创建一个实例;DateTimeFormatter通过ofPattern方法创建对象,是线程安全的,所以在多线程程序中不需要每次使用都创建实例;SimpleDateFormatsdf=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss.SSS");DateTimeFormatterdtf=DateTimeFormatter.ofPattern("yyyy/MM/ddHH:mm:ss.SSS");parse方法:SimpleDateFormat的parse方法将字符串转换为日期;DateTimeFormatter的parse方法将String转换为TemporalAccessor;format方法:SimpleDateFormat的format方法将Date转String;DateTimeFormatter的格式化方法将TemporalAccessor转换为String;参考:https://linuxtut.com/en/935f4…
