Java:LocalDatTime(代替Calendar)、ZoneDateTime(时区时间)
文章目录
- Local(代替Calendar)
- 方法:获取当前
- 代码
- LocalDate(年月日星期)
- LocalTime(时分秒纳秒)
- LocalDateTime(最常用:年月日时分秒纳秒)
- ZoneId 时区
- 表示
- 方法
- ZoneDateTime(时区时间)
- 方法
- 世界标准时间(直接记)
- 修改 with
Local(代替Calendar)
LocalXxx对象是不可变的,每次修改 或者加减 都要新建一个对象
方法:获取当前
代码
package NewTime;
import java.time.LocalDate;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-27
* LocalDate对象是不可变的,每次修改 或者加减 都要新建一个对象
*/
public class LovalTest {
public static void main(String[] args) {
//1. 获取本地的日期对象
LocalDate now = LocalDate.now();
System.out.println(now);
//2. 获取日期对象中的信息
int year = now.getYear();
System.out.println(year);
int month = now.getMonthValue();//1-12
System.out.println(month);
int date = now.getDayOfMonth();
System.out.println(date);
int dayOfWeek = now.getDayOfWeek().getValue();//1-7 星期几
System.out.println(dayOfWeek);
System.out.println("~~~~~~~~~~~");
//3. 直接修改某个信息
LocalDate ld1 = now.withYear(2000);
System.out.println(ld1);
System.out.println(now);
//4. 把某个信息加多少
LocalDate ld2 = now.plusDays(10);
System.out.println(ld2);
LocalDate ld3 = now.plusMonths(2);
System.out.println(ld3);
LocalDate ld4 = ld2.plusYears(2);
System.out.println(ld4);
System.out.println("~~~~~~~~~~~~~~~");
//6. 获取指定日期的LocalDate对象
LocalDate ld5 = LocalDate.of(2024, 9, 1);
System.out.println(ld5);
//5. 把某个信息减多少
LocalDate ld6 = now.minusDays(10);
System.out.println(ld6);
//7. 判断2个日期对象是否相等,在前还是在后
System.out.println(ld5.isAfter(now));
}
}
LocalDate(年月日星期)
LocalTime(时分秒纳秒)
LocalDateTime(最常用:年月日时分秒纳秒)
ZoneId 时区
表示
方法
== 快捷键:查找:ctrl+f==
public static void main(String[] args) {
//ZoneId
//获取系统默认时区 public static ZoneId systemDefault();
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
//获取Java支持的全部时区Id public static Set<String> getAvailableZoneIds();
//因为有时候 不知道某个国家,就可以先把id全查出来, ctrl+f 在下面能搜素
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(availableZoneIds);
//把某个时区的Id封装成ZoneId对象 public staitc ZoneId of(String zoneId)
//查到id,然后封装
ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
}
ZoneDateTime(时区时间)
方法
public static void main(String[] args) {
//ZoneId
//获取系统默认时区 public static ZoneId systemDefault();
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
//获取Java支持的全部时区Id public static Set<String> getAvailableZoneIds();
//因为有时候 不知道某个国家,就可以先把id全查出来, ctrl+f 在下面能搜素
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
System.out.println(availableZoneIds);
//把某个时区的Id封装成ZoneId对象 public staitc ZoneId of(String zoneId)
//查到id,然后封装
ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
//ZoneDateTime
//获取某个时区的ZoneDateTime对象 public static ZoneDateTime now(ZoneId zoneId)
ZonedDateTime now = ZonedDateTime.now(zoneId1);
System.out.println(now);
//获取系统默认的ZoneDateTime对象 public static ZoneDateTime now()
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println(now1);
}
世界标准时间(直接记)
修改 with
public static void main(String[] args) {
//世界标准时间
ZonedDateTime world = ZonedDateTime.now(Clock.systemDefaultZone());
System.out.println(world);
//修改 with
}