java - DateTime

currentTimeMillis

long time = System.currentTimeMillis(); // 返回当前时间与1970年之间的时间差(毫秒) 时间戳
System.out.println(time);

Date

Date d1 = new Date(); // 创建一个对应当前时间戳的Date对象
System.out.println(d1.toString()); // 显示当前年月日时分秒
System.out.println(d1);
System.out.println(d1.getTime()); // 时间戳

Date d2 = new Date(1660812288746L); // 创建指定毫秒数的Date对象
System.out.println(d2);

// 数据库相关
java.sql.Date d3 = new java.sql.Date(1660812288746L);
System.out.println(d3);

SimpleDateFormat

// 空参构造器对象
SimpleDateFormat sim = new SimpleDateFormat();
Date date = new Date();

// 格式化 日期格式转换为字符串
String format = sim.format(date);
System.out.println(format);

// 解析 字符串转换日期
String str = "22-09-09 下午3:30";
Date date1 = sim.parse(str);
System.out.println(date1);
// 指定参数构造器对象
SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = sim1.format(date);
System.out.println(format1);
Calendar
// Calendar 日历类(抽象类)
// 月份 一月是0 十二月是11  周 周日是1 周六是7
// 实例化子类
GregorianCalendar gregorianCalendar = new GregorianCalendar();

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass()); // 调用静态方法的实例也是Calendat子类


// get() 获取指定参数内容  当前时间08-19
int i = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(i); // 19
// set()
calendar.set(Calendar.DAY_OF_MONTH,1);
i = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(i); // 1
// add()
calendar.add(calendar.DAY_OF_MONTH,5);
i = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(i); // 6

// getTome() 获取日期格式时间
Date date = calendar.getTime();

// setTime() 日期格式时间转换日历类
Date date1 = new Date();
calendar.setTime(date1);
i = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(i); // 19
LocalDate LocalTime LocalDateTime
//LocalDate LocalTime LocalDateTime
// 实例化 now() of()
// now 获取日期、时间、日期时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate); // 2022-08-19
System.out.println(localTime); // 16:58:10.319
System.out.println(localDateTime); // 2022-08-19T16:58:10.319

// of 设置指定的 年月日时分秒
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 5, 5, 10, 45, 45);

// getXxx() 获取一些属性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getDayOfWeek());

// withXxx() 设置一些属性
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(5);
System.out.println(localDateTime);
System.out.println(localDateTime2);

// plusXxx() 在当前时间上添加
// minusXxx() 在当前时间上减少
Instant
// Instant
// 实例化 now()
Instant instant = Instant.now(); // 本初子午线
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime); // 东八时区 北京时间
// toEpochMilli() 获取自1970年毫秒数(UTC)
long l = instant.toEpochMilli();
System.out.println(l);

// 根据毫秒数获取时间
Instant instant1 = Instant.ofEpochMilli(1660900963075L);
System.out.println(instant1);
DateTimeFormatter
// DateTimeFormatter
// 预定格式化 ISO_LOCAL_DATE_TIME  ISO_LOCAL_DATE  ISO_LOCAL_TIME
DateTimeFormatter localDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 格式化
String s = localDateTime.format(LocalDateTime.now());
System.out.println(s);
// 解析
TemporalAccessor parse = localDateTime.parse(s);
System.out.println(parse);

// 本地化相关格式 DateTimeFormatter.ofLocalizedDateTime();

// 自定义格式 ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// 格式化
String format = dateTimeFormatter.format(LocalDateTime.now());
System.out.println(format);

// 解析
TemporalAccessor parse1 = dateTimeFormatter.parse(format);
System.out.println(parse1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值