環境:java version "13.0.1"。
創建一個DateUtils類,提供三個常用方法:
-
String 轉換 LocalDateTime的方法。
-
獲取LocalDateTime獲取 年月日時分秒。
-
兩個時間的間隔毫秒數,常用於判斷日期大小。
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
private static DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
public static void getTimeByDate() {
Date date = new Date();
DateFormat df1 = DateFormat.getDateInstance();//日期格式,精確到日
System.out.println(df1.format(date));
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精確到時分秒
System.out.println(df2.format(date));
DateFormat df3 = DateFormat.getTimeInstance();//只顯示出時分秒
System.out.println(df3.format(date));
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //顯示日期,周,上下午,時間(精確到秒)
System.out.println(df4.format(date));
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //顯示日期,上下午,時間(精確到秒)
System.out.println(df5.format(date));
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //顯示日期,上下午,時間(精確到分)
System.out.println(df6.format(date));
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //顯示日期,時間(精確到分)
System.out.println(df7.format(date));
}
public static void getTimeByCalendar() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);//獲取年份
int month = cal.get(Calendar.MONTH);//獲取月份
int day = cal.get(Calendar.DATE);//獲取日
int hour = cal.get(Calendar.HOUR);//小時
int minute = cal.get(Calendar.MINUTE);//分
int second = cal.get(Calendar.SECOND);//秒
int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第幾天
System.out.println("現在的時間是:公元" + year + "年" + month + "月" + day + "日," + hour + "時" + minute + "分" + second + "秒, 一周的第幾天:" + WeekOfYear);
}
/**
* 獲取指定時間的年月日等
* @param localDateTime 指定時間
*/
public static void getTimeByGivenLocalDateTime(LocalDateTime localDateTime) {
if (null == localDateTime) {
localDateTime = LocalDateTime.now();
}
int dayOfYear = localDateTime.getDayOfYear();
int dayOfMonth = localDateTime.getDayOfMonth();
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("今天是" + localDateTime + "\n"
+ "本年當中第" + dayOfYear + "天" + "\n"
+ "本月當中第" + dayOfMonth + "天" + "\n"
+ "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n");
//獲取指定時間的年月日時分秒
int year = localDateTime.getYear();
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
System.out.println("今天是" + localDateTime + "\n"
+ "年 : " + year + "\n"
+ "月 : " + month.getValue() + "-即 " + month + "\n"
+ "日 : " + day + "\n"
+ "時 : " + hour + "\n"
+ "分 : " + minute + "\n"
+ "秒 : " + second + "\n"
);
}
/**
* String 轉換 LocalDateTime
* @param dateStr
* @return
*/
public static LocalDateTime string2Time(String dateStr) {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
LocalDateTime time = LocalDateTime.parse(dateStr, fmt);
System.out.println("time:" + time);
return time;
}
/**
* 求時間間隔,返回值大於零說明結束時間晚於開始時間
* @param beginTime 開始時間
* @param endTime 結束時間
* @return 毫秒,等於endTime 減去 beginTime后的毫秒數
*/
public static Long between(LocalDateTime beginTime, LocalDateTime endTime) {
Duration duration = Duration.between(beginTime, endTime);
return duration.toMillis();
}
public static void main(String[] args) {
getTimeByDate();
System.out.println("********getTimeByCalendar********************");
getTimeByCalendar();
System.out.println("********getTimeByGivenLocalDateTime****************");
String str = "2021-10-09 15:32:06:777";
LocalDateTime ldt = string2Time(str);
getTimeByGivenLocalDateTime(ldt);
System.out.println("********between****************");
str = "2021-10-09 17:32:06:777";
LocalDateTime endTime = string2Time(str);
System.out.println(between(ldt, endTime));
}
}
測試結果:
2021年10月10日
2021年10月10日 下午12:06:02
下午12:06:02
2021年10月10日星期日 中國標准時間 下午12:06:02
2021年10月10日 CST 下午12:06:02
2021/10/10 下午12:06
2021年10月10日 下午12:06:02
********getTimeByCalendar********************
現在的時間是:公元2021年9月10日,0時6分2秒, 一周的第幾天:1
********getTimeByGivenLocalDateTime****************
time:2021-10-09T15:32:06.777
今天是2021-10-09T15:32:06.777
本年當中第282天
本月當中第9天
本周中星期6-即SATURDAY
今天是2021-10-09T15:32:06.777
年 : 2021
月 : 10-即 OCTOBER
日 : 9
時 : 15
分 : 32
秒 : 6
********between****************
time:2021-10-09T17:32:06.777
7200000
Process finished with exit code 0
