日期Api

获取年/月/日 时:分:秒

Calendar cl = Calendar.getInstance();
int year = cl.get(Calendar.YEAR);
int month = cl.get(Calendar.MONTH); //0-11
int day = cl.get(Calendar.DATE);
int hour = cl.get(Calendar.HOUR);
int minute = cl.get(Calendar.MINUTE);
int second = cl.get(Calendar.SECOND);
System.out.println(year +"/"+(month+1) + "/" + day +" " + hour + ":" + minute + ":" + second);

//jdk1.8
LocalDateTime now = LocalDateTime.now();
int year1 = now.getYear();
int month1 = now.getMonth().getValue();
int day1 = now.getDayOfMonth();
int hour1 = now.getHour();
int minute1 = now.getMinute();
int second1 = now.getSecond();
System.out.println(year1 +"/"+month1 + "/" + day1 +" " + hour1 + ":" + minute1 + ":" + second1);

获取从1970/1/1 0:0:0到现在的毫秒数

System.out.println("第一种方式:"+Calendar.getInstance().getTimeInMillis());
System.out.println("第二种方式:"+System.currentTimeMillis());
System.out.println("第三种方式:"+Clock.systemDefaultZone().millis());

获得本月的最后一天和第一天

//获得当月的第一天
Calendar cl2 = Calendar.getInstance();
cl2.add(Calendar.MONTH,0);
cl2.set(Calendar.DAY_OF_MONTH,1);   //设置为1号,当前日期即为本月第一天
String first = sdf.format(cl2.getTime());
System.out.println("本月的第一天:" + first);

//获得本月的最后一天
Calendar cl3 = Calendar.getInstance();
cl3.set(Calendar.DAY_OF_MONTH, cl3.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = sdf.format(cl3.getTime());
System.out.println("本月的最后一天:" + last);

//jdk1.8
LocalDate now1 = LocalDate.now();
//本月第一天
LocalDate firstDay = LocalDate.of(now1.getYear(), now1.getMonth(), 1);
//最后一天
LocalDate lastDay = now1.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月的第一天:" + firstDay);
System.out.println("本月的第一天:" + lastDay);

打印本年的12月份的日期

Calendar cl4 = Calendar.getInstance();
cl4.set(Calendar.YEAR,2020);
for(int i = 0; i < 12; i++){
    cl4.set(Calendar.MONTH,i);
    System.out.println("今年"+(i+1)+"月份: " + cl4.getActualMaximum(Calendar.DATE));
}

格式化日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatDate = sdf.format(new Date());
System.out.println(formatDate);
//jdk1.8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formatDate2 = dtf.format(LocalDate.now());
System.out.println(formatDate2);

打印昨天的当前时刻

Calendar c = Calendar.getInstance();
c.add(Calendar.DATE,-1);
System.out.println(c.getTime());

//jdk1.8
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = now.minusDays(1);
System.out.println(localDateTime);

LocalDateTime与Date之间的转换

//LocalDate -> Date
Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

//LocalDateTime -> Date
Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

//Date -> LocalDate
Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
 
//Date -> LocalDateTime
Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();

Date、LocalDate、LocalDateTime与时间戳互转

// Date ---> TimeStamp
Date now = new Date();
long t1 = now.getTime();

// LocalDate ---> TimeStamp
long t2 = LocalDate.now().atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();

// LocalDateTime ---> TimeStamp
long t3 = LocalDateTime.now().toInstant(ZoneOffset.ofHours(0)).toEpochMilli();
----------------------------------------
// TimeStamp ---> Date
Date date = new Date(timeStamp);

// TimeStamp ---> LocalDate
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();

// TimeStamp ---> LocalDateTime
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值