Java的日期時間類是我一直想吐槽的。Date.getYear()這麼好用的方法爲什麼要標記爲deprecated?然後換成Calendar、LocalDateTime也不見得有多好用。總之是反人類的。寫一個好用的日期時間類就那麼困難嗎?創造Java的大師們肯定比我厲害,但依然阻止不了我想罵人的想法。
package aaa;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 好用的日期類
* 1 清除時間部分 clearTime()
* 2 加日期 加小時 addDate(2) addHours(2)
* 3 字符串轉日期時間 parseString(s) parseString(s, format)
* 4 日期時間轉字符串 toString() toString(format) toDateString() toTimeString()
* 5 獲取日期時間各個部分
* 6 設置日期時間各個部分
* 7 日期相減 dateDiff(d)
*
* @author 醫手
*/
public class MyDate extends Date {
public static void main(String[] args) {
MyDate d = new MyDate();
System.out.println(d); // 2020-03-20 08:56:14
System.out.println(d.toString("yyyy年MM月dd日")); // 2020年03月20日
d.parseString("2006-01-02 13:04:05");
System.out.println(d); // 2006-01-02 13:04:05
System.out.println(d.getYear()); // 2006
System.out.println(d.getMonth()); // 1 1月爲1
System.out.println(d.getDate()); // 2
System.out.println(d.getDay()); // 1 星期天爲0 星期一爲1
System.out.println(d.getHours()); // 13
System.out.println(d.getMinutes()); // 4
System.out.println(d.getSeconds()); // 5
d.setYear(2019);
d.setMonth(6);
d.setDate(16);
d.setHours(19);
d.setMinutes(26);
d.setSeconds(33);
System.out.println(d); // 2019-06-16 19:26:33
d.addDate(2);
d.addHours(2);
System.out.println(d); // 2019-06-18 21:26:33
d.setDate(2006, 1, 2);
d.setTime(13, 4, 5);
System.out.println(d); // 2006-01-02 13:04:05
d.clearTime();
System.out.println(d); // 2006-01-02 00:00:00
d.setTime(60 * 1000);
System.out.println(d); // 1970-01-01 08:01:00
d.parseString("2006年01月02日 13:04:05", "yyyy年MM月dd日 HH:mm:ss");
System.out.println(d); // 2006-01-02 13:04:05
d.parseString("2006-01-02");
System.out.println(d); // 2006-01-02 00:00:00
d.parseString("13:04:05");
System.out.println(d); // 1970-01-01 13:04:05
System.out.println(d.toDateString()); // 1970-01-01
System.out.println(d.toTimeString()); // 13:04:05
d.setTime(23, 59, 59);
System.out.println(d); // 1970-01-01 23:59:59
MyDate d2 = new MyDate(d);
d2.addDate(1);
d2.clearTime();
System.out.println(d2); // 1970-01-02 00:00:00
System.out.println(d2.dateDiff(d)); // 1 忽略時間部分
}
public Calendar cal;
public SimpleDateFormat formatFull;
public SimpleDateFormat formatDate;
public SimpleDateFormat formatTime;
private void initial(Date date) {
cal = Calendar.getInstance();
if (date != null) {
cal.setTime(date);
}
this.setTime(cal.getTimeInMillis());
formatFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatDate = new SimpleDateFormat("yyyy-MM-dd");
formatTime = new SimpleDateFormat("HH:mm:ss");
}
public MyDate() {
initial(null);
}
public MyDate(Date date) {
initial(date);
}
/**
* 獲取年
*/
public int getYear() {
return cal.get(Calendar.YEAR);
}
/**
* 獲取月 1月爲1
*/
public int getMonth() {
return cal.get(Calendar.MONTH) + 1;
}
/**
* 獲取在月份中的日期
*/
public int getDate() {
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 獲取星期 星期天爲0 星期一爲1
*/
public int getDay() {
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 獲取小時
*/
public int getHours() {
return cal.get(Calendar.HOUR_OF_DAY);
}
/**
* 獲取分鐘
*/
public int getMinutes() {
return cal.get(Calendar.MINUTE);
}
/**
* 獲取分鐘
*/
public int getSeconds() {
return cal.get(Calendar.SECOND);
}
/**
* 設置年
*/
public void setYear(int value) {
cal.set(Calendar.YEAR, value);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置月 1月爲1
*/
public void setMonth(int value) {
cal.set(Calendar.MONTH, value - 1);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置在月份中的日期
*/
public void setDate(int value) {
cal.set(Calendar.DAY_OF_MONTH, value);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置年月日
*/
public void setDate(int year, int month, int date) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, date);
this.setTime(cal.getTimeInMillis());
}
/**
* 增加日期
*/
public void addDate(int value) {
cal.set(Calendar.DAY_OF_MONTH, this.getDate() + value);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置小時
*/
public void setHours(int value) {
cal.set(Calendar.HOUR_OF_DAY, value);
this.setTime(cal.getTimeInMillis());
}
/**
* 增加小時
*/
public void addHours(int value) {
cal.set(Calendar.HOUR_OF_DAY, this.getHours() + value);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置分鐘
*/
public void setMinutes(int value) {
cal.set(Calendar.MINUTE, value);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置秒
*/
public void setSeconds(int value) {
cal.set(Calendar.SECOND, value);
cal.set(Calendar.MILLISECOND, 0);
this.setTime(cal.getTimeInMillis());
}
/**
* 設置時分秒
*/
public void setTime(int hour, int menute, int second) {
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, menute);
cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, 0);
this.setTime(cal.getTimeInMillis());
}
/**
* 清除時分秒
*/
public void clearTime() {
int value = 0;
cal.set(Calendar.HOUR_OF_DAY, value);
cal.set(Calendar.MINUTE, value);
cal.set(Calendar.SECOND, value);
cal.set(Calendar.MILLISECOND, value);
this.setTime(cal.getTimeInMillis());
}
/**
* 轉字符串 2006-01-02 13:04:05
*/
@Override
public String toString() {
return formatFull.format(this);
}
/**
* 轉字符串
*
* @param format yyyy-MM-dd HH:mm:ss.S
* @return
*/
public String toString(String format) {
SimpleDateFormat f = new SimpleDateFormat(format);
return f.format(this);
}
/**
* 轉字符串 2006-01-02
*/
public String toDateString() {
return formatDate.format(this);
}
/**
* 轉字符串 13:04:05
*/
public String toTimeString() {
return formatTime.format(this);
}
/**
* 字符串轉時間 支持以下三種
* 2006-01-02 13:04:05
* 2006-01-02
* 13:04:05
*
* @param source
*/
public void parseString(String source) {
try {
Date date = null;
boolean hasDate = source.indexOf("-") != -1;
boolean hasTime = source.indexOf(":") != -1;
if (hasDate && hasTime) {
date = formatFull.parse(source);
} else if (hasDate) {
date = formatDate.parse(source);
} else if (hasTime) {
date = formatTime.parse(source);
} else {
throw new RuntimeException("無效的時間:" + source);
}
this.setTime(date.getTime());
cal.setTime(this);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
/**
* 字符串轉時間
*
* @param source
* @param format yyyy-MM-dd HH:mm:ss.S
*/
public void parseString(String source, String format) {
try {
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = f.parse(source);
this.setTime(date.getTime());
cal.setTime(this);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
/**
* 日期相減
* 忽略時間部分
*
* @param date
* @return 天數
*/
public int dateDiff(Date date) {
long oneDay = 24 * 60 * 60 * 1000;
long offset = cal.get(Calendar.ZONE_OFFSET);
long diff = (this.getTime() + offset) / oneDay - (date.getTime() + offset) / oneDay;
return (int) diff;
}
}