Java8之前的传统日期API
Date转换成字符串
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/7 14:43
*/
public class Demo {
public static void main(String[] args) {
//创建Date类,构造的时候填入当前的日期
Date date = new Date();
System.out.println(date);
//获得秒
final long time = date.getTime();
System.out.println(time);
//将Date转换成更好识别的日期字符串
final SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E a");
final String format1 = format.format(date);
System.out.println(format1);
//将秒转换成更好识别的日期字符串
final String format2 = format.format(time);
System.out.println(format2);
}
}
字符串解析成Date对象
//将字符串的时间解析成日期对象
String str = "2024-03-07 15:46:50";
final SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
final Date parse = format3.parse(str);
System.out.println(parse);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar对象
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/7 14:43
*/
public class Demo {
public static void main(String[] args) {
final Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
//输出今天时今年的第几天
final int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(dayOfYear);
//修改年份
calendar.set(Calendar.YEAR, 2025);
//获取输出年份
final int year = calendar.get(Calendar.YEAR);
System.out.println(year);
}
}
Java开始新增的日期和时间
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/7 14:43
*/
public class Demo {
public static void main(String[] args) {
final Date date = new Date();
final int year = date.getYear();
//1.设计不合理,使用不方便
System.out.println(year + 1900); //这里得到的年份还要-1900
Calendar cal = Calendar.getInstance();
final int month = cal.get(Calendar.MONTH); //这里得到月份还要自己记忆Calendar.MONTH
System.out.println(month);
//2.传统的日期都是可变的对象,修改后丢失原始的信息
//3.传统的日期是线程不安全的
}
}
分类
LocalDate
import java.time.DayOfWeek;
import java.time.LocalDate;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/7 16:42
*/
public class LocalDateDemo {
public static void main(String[] args) {
//获取本地日期对象(不可变对象)
final LocalDate now = LocalDate.now();
//1.获取对象中的信息
System.out.println(now); //2024-03-07
final int year = now.getYear();
System.out.println(year); //2024
final int month = now.getMonthValue();
System.out.println(month); //3
final int day = now.getDayOfMonth();
System.out.println(day); //7
final int dayOfYear = now.getDayOfYear();
System.out.println(dayOfYear);//67
final DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println(dayOfWeek); //THURSDAY
//2.直接修改某个信息
final LocalDate now1 = now.withYear(2099);
final LocalDate now2 = now.withMonth(12);
System.out.println(now); //2024-03-07
System.out.println(now1); //2099-03-07
System.out.println(now2); //2024-12-07
final LocalDate now3 = now.plusYears(2); //2026-03-07
System.out.println(now3);
final LocalDate now4 = now.minusYears(2);
System.out.println(now4); //2022-03-07
final LocalDate now5 = LocalDate.of(2008, 12, 12);
final LocalDate now6 = LocalDate.of(2008, 12, 22);
System.out.println(now5); //2008-12-12
System.out.println(now6); //2008-12-22
System.out.println(now5.equals(now6)); //false
System.out.println(now5.isBefore(now6)); //true
System.out.println(now5.isAfter(now6)); //false
}
}
LocalTime
import java.time.LocalTime;
/**
* @author Pickle
* @version V1.0
* @date 2024/3/7 18:38
*/
public class LocalTimeDemo {
public static void main(String[] args) {
final LocalTime now = LocalTime.now();
System.out.println(now); //18:38:47.606559700
//1.获取事件中的信息
System.out.println(now.getHour()); //时
System.out.println(now.getMinute()); //分
System.out.println(now.getSecond()); //秒
System.out.println(now.getNano()); //纳秒
//2.修改时间 withHour,withMinute,withSecond,withNano
//3.加时间 plusHours,plusMinutes,plusSecond,plusNanos
//4.减多少
//...同LocalDate
}
}
LocalDateTime
和LocalDate、LocalDateTime类似
但是LocalDateTime可以分成LocalDate和LocalTime,LocalTime和LocalDate也可以合成LocalDateTime