目錄
1、Date
1、Date格式化工具——SimpleDateFormate用于時間的格式化
2、SimpleDateFormate中的常用方法
2、LocalDate與LocalTime?
1、LocalDate的常用方法
2、LocalTime的創(chuàng)建、構(gòu)造
3、LocalDate、LocalTime格式化工具——DateTimeFormatter
4、parse()與format()
3、calendar
4、時間戳
1、時間戳轉(zhuǎn)換為自定義格式的時間字符串
1、時間戳轉(zhuǎn)換為Date
5、時間間隔計算
1、Date
1、Date格式化工具——SimpleDateFormate用于時間的格式化
????????1、yyyy-MM-dd HH:mm:ss(24小時制)
????????2、yyyy-MM-dd HH:mm:ss(12小時制)
? ? ? ? 3、yyyy-MM-dd HH:mm:ss.SSS(年-月-日 時:分:秒.毫秒)
2、SimpleDateFormate中的常用方法
????????format(Date date)方法實現(xiàn)Date數(shù)據(jù)、Calendar數(shù)據(jù)轉(zhuǎn)換為指定格式的字符串
????????parse(String dateStr)方法將時間格式的String轉(zhuǎn)化為Date數(shù)據(jù)
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
public static void main(String[] args) {
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:"+df.format(d));
System.out.println("兩天前的日期:" + df.format(new Date(d.getTime() - (long)2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + (long)3 * 24 * 60 * 60 * 1000)));
}
}
2、LocalDate與LocalTime?
LocalDate無法包含時間,LocalTime無法包含日期,LocalDateTime才能同時包含時間和日期。
1、LocalDate的常用方法
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class demo {
public static void main(String[] args) throws ParseException {
// 獲取今天的日期
LocalDate today = LocalDate.now();
// 今天是本月幾號
int dayOfMonth = today.getDayOfMonth();
// 今天是周幾(返回的是個枚舉類型,需要再getValue())
int dayOfWeek = today.getDayOfWeek().getValue();
// 今天是今年中的第幾天
int dayOfYear = today.getDayOfYear();
// 根據(jù)字符串取:嚴(yán)格按照yyyy-MM-dd驗證,02寫成2都不行,如何想改變格式通過DateTimeFormatter實現(xiàn)
LocalDate endOfFeb = LocalDate.parse("2023-02-28");
// 取本月第1天
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月第3天
LocalDate secondDayOfThisMonth = today.withDayOfMonth(3);
// 取本月最后一天,不用計算是28,29,30還是31
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// 取下一天
LocalDate firstDayOfNextMonth = today.plusDays(1);
// 取2023年9月第一個周一
LocalDate firstMondayOf2023 = LocalDate.parse("2023-09-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstDayOfNextMonth);
}
}
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
public class demo {
public static void main(String[] args) {
LocalDate beginTime = LocalDate.now();
// 月份運算
LocalDate entTime = beginTime.plus(Period.ofMonths(6));
// LocalDate 轉(zhuǎn) Date
Date date = Date.from(beginTime.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
}
2、LocalTime的創(chuàng)建、構(gòu)造
import java.time.LocalTime;
public class demo {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
// 構(gòu)造
LocalTime zero = LocalTime.of(0, 0, 0);
LocalTime mid = LocalTime.parse("12:00:00");
}
}
3、LocalDate、LocalTime格式化工具——DateTimeFormatter
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class demo {
public static void main(String[] args) throws ParseException {
LocalDate today = LocalDate.now();
DateTimeFormatter formatters1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
//formatters1 與 today 位置可以互換
String text = formatters1.format(today);
LocalTime now = LocalTime.now();
//formatters2 與 now 位置可以互換
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("hh mm ss.SSS");
String midStr = mid.format(formatter2);
System.out.println(midStr);
}
}
4、parse()與format()
parse調(diào)用 | format調(diào)用 | |
SimpleDateFormate | 1、創(chuàng)建SimpleDateFormate,確定格式 2、傳入時間字符串,sdf.parse(timeStr) |
1、創(chuàng)建SimpleDateFormate,確定格式 2、sdf.format(date) |
LocalDate | LocalDate.parse(timeStr) 字符串格式嚴(yán)格按照yyyy-MM-dd驗證 |
1、創(chuàng)建DateTimeFormatter,確定格式 2、dtf.format(dateStr) |
LocalTime | LocalTime.parse(timeStr) | 1、創(chuàng)建DateTimeFormatter,確定格式 2、dtf.format(timeeStr) |
3、calendar
getTime()獲取時間,add()修改時間,get()獲取指定部分時間
import java.util.Calendar;
public class demo {
public static void main(String[] args) {
// 使用默認(rèn)時區(qū)和語言環(huán)境獲得一個日歷
Calendar cal = Calendar.getInstance();
Date nowTime = calendar.getTime();
calendar.add(Calendar.DATE, 5);
Date startTime = calendar.getTime();
// 賦值時年月日時分秒常用的6個值,注意月份下標(biāo)從0開始,所以取月份要+1
System.out.println("年:" + cal.get(Calendar.YEAR));
System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));
System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("時:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("分:" + cal.get(Calendar.MINUTE));
System.out.println("秒:" + cal.get(Calendar.SECOND));
}
}
4、時間戳
Unix時間戳(Unix timestamp)定義為從1970年01月01日00時00分00秒(UTC)起至現(xiàn)在經(jīng)過的總秒數(shù),是我們北京時間的1970年01月01日08時00分00秒文章來源:http://www.zghlxwxcb.cn/news/detail-777869.html
13位時間戳精確到毫秒,10位精確到秒文章來源地址http://www.zghlxwxcb.cn/news/detail-777869.html
1、時間戳轉(zhuǎn)換為自定義格式的時間字符串
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
public static void main(String[] args) {
//獲取當(dāng)前毫秒級時間戳
Long time = System.currentTimeMillis();
System.out.println(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//返回的是指定格式的string類型時間
String date = sdf.format(time);
System.out.println(date);
}
1、時間戳轉(zhuǎn)換為Date
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
public static void main(String[] args) {
//獲取當(dāng)前毫秒級時間戳
Long time = System.currentTimeMillis();
Date date = new Date(time);
System.out.println(date);
}
5、時間間隔計算
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// 定義兩個時間
String startTime = "1949-10-1 00:00:00.000";
String endTime = "2023-8-21 20:10:10.010";
// 將兩個String類型的時間轉(zhuǎn)換為Date類型,從而計算差值、parse()方法的作用是將String類型的時間解析為Date類型
Date d1 = df.parse(startTime);
Date d2 = df.parse(endTime);
System.out.println((((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000))) / 365 + "年");
// 以下總和為時間差
System.out.println((d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000) + "天");
System.out.println((((d2.getTime() - d1.getTime()) / (60 * 60 * 1000)) % 24) + "小時");
System.out.println((((d2.getTime() - d1.getTime()) / 1000) % 60) + "分鐘");
System.out.println(((d2.getTime() - d1.getTime()) / (60 * 1000)) % 60 + "秒");
System.out.println((((d2.getTime() - d1.getTime())) % 1000) + "毫秒");
}
}
到了這里,關(guān)于java中Date、LocalDate、LocalTime、時間戳的相互轉(zhuǎn)化和基本使用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!