转换过程需要两个辅助类:
import java.text.SimpleDateFormat; import java.util.Date;
下面是java 编写的案例:
package com.example.formatetime; import java.text.SimpleDateFormat; import java.util.Date; public class myClass { public static void main(String[] args){ long tempTime = System.currentTimeMillis()/*1522290924000L*/; Date date = new Date(tempTime); SimpleDateFormat mSimpleDateFormat1 = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat mSimpleDateFormat3 = new SimpleDateFormat("HH:mm:ss"); SimpleDateFormat mSimpleDateFormat4 = new SimpleDateFormat("YYYY:mm:DD hh:MM:SS"); System.out.println("result1 = "+mSimpleDateFormat1.format(date)); System.out.println("result2 = "+mSimpleDateFormat2.format(date)); System.out.println("result3 = "+mSimpleDateFormat3.format(date)); System.out.println("result4 = "+mSimpleDateFormat4.format(date)); } }
编译结果
result1 = 2018:04:03 13:44:26 result2 = 2018/04/03 result3 = 13:44:26 result4 = 2018:44:93 01:04:264
需要注意:给定的时间格式("yyyy:MM:dd HH:mm:ss")是区分大小写的
year: y or Y, 实验大小写, 无差别。
month: M or m. M 年份中的月份; m 暂时不详。
day: D or d. D 年份中的天数; d 月份中的天数。
hour: H or h. H 24进制; h 12进制。
minutes: M or m. M 暂时不详; m 小时内分钟。
second: S or s. S 暂时不详; s分钟内秒数。
北京时间:月份和小时是大写,其它小写。