自写 System.out.currentTimeMillis() 到 年月日时分秒 的转换


前言

有时只是想简单地将System.currentTimeMillis()转为本地的
年-月-日 时:分:秒 , 感觉用LocaDateTime有点庞大, 翻看java.util.Date的代码,感觉还是有点庞大,好几个类,上万行代码. 于是想自己写个最简单的转换类, 达到效果就行 ,快速轻便 , 过程中发现日期真不是很好弄, 得弄懂公元,闰年,4年闰,100年不闰,400年闰的逻辑就让人想炸裂, 有些时期还使用夏令时, 夏令时就不管了, 过时产物


使用java自带工具的简单方法收集

java.text.SimpleDateFormat

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(
				f.format(System.currentTimeMillis())
				+"\n"
				+f.format(new Date())
				);

java.time.format.DateTimeFormatter

		DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println(
				f2.format(LocalDateTime.now())
				+"\n"
				+f2.format(ZonedDateTime.now())
				+"\n"
				+f2.format(LocalDateTime.now(ZoneId.systemDefault()))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.systemDefault()))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Asia/Chongqing")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Asia/Chungking")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Asia/Hong_Kong")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Hongkong")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("Etc/GMT-8")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("GMT+8")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("GMT+08")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("GMT+08:00")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UTC+8")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UTC+08")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UTC+08:00")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UT+8")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UT+08")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("UT+08:00")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("+8")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("+08")))
				+"\n"
				+f2.format(ZonedDateTime.now(ZoneId.of("+08:00")))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getDefault().toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("GMT+8").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("GMT+8:00").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("GMT+08:00").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("Asia/Shanghai").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("Asia/Chongqing").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("Asia/Chungking").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("Asia/Hong_Kong").toZoneId()))
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone("CTT").toZoneId())) //TimeZone可用CTT代表上海,ZoneId不可以
				+"\n"
				+f2.format(ZonedDateTime.now(TimeZone.getTimeZone(ZoneId.of("GMT+8")).toZoneId()))
				+"\n"
				+ZoneId.getAvailableZoneIds()
				);

java.text.SimpleDateFormat

		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.out.println(
				timestamp.toString().substring(0, 19)
				);

java.sql.Date 和 java.sql.Time

		java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
		java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());
		System.out.println(sqlDate+" "+sqlTime);

自写代码1

1900至2000不考虑夏令时与LocalDateTime结果验证一致 2000至2099与LocalDateTime结果验证一致 创建速度比java.util.Date快3倍 , 取值更改快
/**
 * 
 * @author Kfepiza
 * 主要用于System.currentTimeMillis()快速拆分为年月日时分秒毫秒
 * 创建速度比java.util.Date快3倍 , 取值更改快
 * 1900至2000不考虑夏令时与LocalDateTime结果验证一致
 * 2000至2099与LocalDateTime结果验证一致
 */
public class ZbDtt {
	
	final static long T1900 = -2209017600000L ;
	final static long T1970 = -28800000L ;
	final static long BaseTime = T1900 ;
	final static long _1Day = 86400000L;
	final static long _365 = _1Day*365L;
	final static long Year4 = _1Day*(365*4+1);
	
	long fastTime;
	boolean gregorian;
	int year , month , dayOfYear , dayOfMonth , hour , minute , second , ms;
	
	
	public ZbDtt(long t) {
		fastTime = t; ms= t>=0 ? (int) (t%1000) : (int)(10000-(-t%1000))%1000;
		long minusBase=t-BaseTime;
		long yearCount4 = minusBase/Year4;
		long minusLast4 = minusBase%Year4;
		long yearCount1 = minusLast4/_365;
		long minusLast1 = minusLast4%_365;
		long yearMinusBase =yearCount4*4 + yearCount1;
		
		year = (int) (yearMinusBase) + 1900;
		gregorian = isGregorianLeapYear(year);
		int dayTemp = dayOfYear = (int) (minusLast1/_1Day)+1;
		
		if(yearCount1==4 && minusLast1<_1Day) {month=1; dayOfMonth=1; }
		else {
			if(yearCount1==4) {month=1 ; dayOfMonth=2; }
			else {
				if(gregorian)dayTemp++;
				if(dayTemp<32) {month=1; dayOfMonth=dayTemp;}
				else {
					if(dayTemp<60) {month=2; dayOfMonth=dayTemp-31;}
					else { 
						if(gregorian)dayTemp--;
						if(dayTemp==59) {month=2; dayOfMonth=29; }
						else if(dayTemp<91) {month=3; dayOfMonth=dayTemp-59; }
						else if(dayTemp<121) {month=4; dayOfMonth=dayTemp-90; }
						else if(dayTemp<152) {month=5; dayOfMonth=dayTemp-120; }
						else if(dayTemp<182) {month=6; dayOfMonth=dayTemp-151; }
						else if(dayTemp<213) {month=7; dayOfMonth=dayTemp-181; }
						else if(dayTemp<244) {month=8; dayOfMonth=dayTemp-212; }
						else if(dayTemp<274) {month=9; dayOfMonth=dayTemp-243; }
						else if(dayTemp<305) {month=10; dayOfMonth=dayTemp-273; }
						else if(dayTemp<335) {month=11; dayOfMonth=dayTemp-304; }
						else if(dayTemp<366) {month=12; dayOfMonth=dayTemp-334; }
					}
				}
			}
		}
		long hmsms = minusLast1%_1Day;
		hour = (int) (hmsms / 3600000);
		int msms = (int) (hmsms - hour*3600000);
		minute = msms / 60000;
		int sms = msms - minute*60000;
		second = sms / 1000;
	}
	
	public ZbDtt() {this(System.currentTimeMillis());}
	
	
	public String toString() {return year+"-"+month+"-"+dayOfMonth+" "+hour+":"+minute+":"+second; }
	
	public final boolean isGregorianLeapYear() {return gregorian;}
	
    public static final boolean isGregorianLeapYear(int gregorianYear) {
        return (((gregorianYear % 4) == 0)
                && (((gregorianYear % 100) != 0) || ((gregorianYear % 400) == 0))
                );
    }

自写代码2

参考了 java.sql的Date和Time 只是让java.util.Date更符合自己使用习惯

public class Date1 extends java.util.Date{

	
	private static final long serialVersionUID = 7810651682610771465L;
	
	
	public String yyyy , yy , MM , dd , HH , mm , ss ;
	
	public int year , month , day , hour , minute , second , millis;
	
	{
		year = super.getYear()+1900;
		month = super.getMonth() + 1;
        day = super.getDate();
        hour = super.getHours();
        minute = super.getMinutes();
        second = super.getSeconds();
        
        
        yyyy = String.valueOf(year); yy = yyyy.substring(2);
        MM = (month>9) ? String.valueOf(month) : ("0"+month);
        dd = day>9 ? String.valueOf(day) : "0"+day ;
        HH = hour>9 ? String.valueOf(hour) : "0" +hour;
        mm = minute>9 ? String.valueOf(minute) : "0"+minute;
        ss = second>9 ? String.valueOf(second) : "0"+second;
	}
	
	
	public Date1()          {super();}
	public Date1(long time) {super(time);}
	
	
	public String yyyy_MM_dd__HH_mm_ss() {return yyyy+"-"+MM+"-"+dd+" "+HH+":"+mm+":"+ss;}
	@Override
	public String toString() { return  yyyy_MM_dd__HH_mm_ss(); }
	public String supperToString() { return super.toString(); }
	
	public static void main(String arguments[]) {
		System.out.println(new Date1()); 
		long l1 = System.currentTimeMillis();
		System.out.println(l1);
		System.out.println(new Date1(0));
		System.out.println(new Timestamp(l1));
	}
	

}

代码3

还没开始



总结

头大

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值