java获取时间戳精确到毫秒_java获取时间戳精确到微秒讲解

java获取时间戳精确到毫秒_java获取时间戳精确到微秒讲解,第1张

java获取时间戳精确到毫秒_java获取时间戳精确到微秒讲解 当前时间加上时间戳关于日期的计算,很多朋友们都喜欢用时间戳来直接相加,比如希望得到当前时间之后30天的时间,会这么写代码: 直接把 new Data().getTime() 方法得到的时间戳加上30天对应的毫秒数,也就是30天 * 24小时 * 3600秒 * 1000毫秒 Date today = new Date(); Date nextMonth = new Date(today.getTime()+30*24*3600*1000); System.out.println(today); System.out.println(nextMonth);得到的结果可能会让我失败:Sat Jul 10 07:41:30 CST 2021Sun Jun 20 14:38:43 CST 2021得到的日期竟然比当前的日期还好早,你说怎么回事呢?原因是:因为 int 发生了溢出怎样修改呢?我们只要把 30改成30L即可,让其成为long Date today = new Date(); Date nextMonth = new Date(today.getTime()+30L*24*3600*1000); System.out.println(today); System.out.println(nextMonth);结果:Sat Jul 10 07:44:38 CST 2021Mon Aug 09 07:44:38 CST 2021使用Calendar在java8之前,我们一般使用Calendar类来实现 Calendar c = Calendar.getInstance(); c.setTime(new Date()); System.out.println(c.getTime()); c.add(Calendar.DAY_OF_MONTH,30); System.out.println(c.getTime());结果:Sat Jul 10 07:47:25 CST 2021Mon Aug 09 07:47:25 CST 2021java 8 日期时间使用 Java 8 的日期时间类型,可以直接进行各种计算,更加简洁和方便: LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime.plusDays(30));下面介绍LocalDateTime的基本用法:1) 获取一年、月、日 private LocalDateTime localDateTime = null; @Before public void init(){ localDateTime = LocalDateTime.now(); } @Test public void test1(){ System.out.println("Year:"+localDateTime.getYear()); System.out.println("Month:"+localDateTime.getMonth().getValue()); System.out.println("Day of Month:"+localDateTime.getDayOfMonth()); System.out.println("Day of Week:"+ localDateTime.getDayOfWeek()); System.out.println("Day of Year:"+localDateTime.getDayOfYear()); }结果:Year:2021Month:7Day of Month:10Day of Week:SATURDAYDay of Year:191 获取小时、分钟、秒 System.out.println("Hour:"+localDateTime.getHour()); System.out.println("Minute :"+localDateTime.getMinute()); System.out.println("Second:"+localDateTime.getSecond()); System.out.println("Nano:"+localDateTime.getNano());结果:Hour:8Minute :17Second:32Nano:50000000对日期做加减可以使用各种 minus 和 plus 方法直接对日期进行加减 *** 作,比如如下代码实现了减一天和加一天,以及减一个月和加一个月 System.out.println("minus days:"+ localDateTime.minusDays(1)); System.out.println("minus months:"+localDateTime.minusMonths(1)); System.out.println("minus year: "+localDateTime.minusYears(1)); System.out.println("minus Hours:"+localDateTime.minusHours(1)); System.out.println("minus seconds:"+localDateTime.minusSeconds(1));时间的比较LocalDateTime 类提供以下API比较LocalDateTime 对象在Java中。

boolean isAfter(ChronoLocalDateTime other):检查此日期时间是否在指定日期时间之后。

boolean isBefore(ChronoLocalDateTime other)boolean isEqual(ChronoLocalDateTime other)int compareTo(ChronoLocalDateTime other) 将此日期时间与其他日期时间进行比较。

LocalDateTime dateTime1 = LocalDateTime.of(2021,5,7,9,22,22); LocalDateTime dateTime2 = LocalDateTime.of(2021,6,7,9,22,22); LocalDateTime dateTime3 = LocalDateTime.of(2021,5,7,9,22,22); if(dateTime1.isBefore(dateTime2)){ System.out.println("dateTime1 is before dateTime2"); } if(dateTime2.isAfter(dateTime3)){ System.out.println("dateTime2 is after dateTime3"); } if(dateTime1.equals(dateTime3)){ System.out.println("dateTime1 is equal to dateTime3"); } if(dateTime1.compareTo(dateTime3) ==0){ System.out.println("dateTime1 is equal to dateTime3"); }通过 with 方法进行快捷时间调节使用 TemporalAdjusters.firstDayOfMonth 得到当前月的第一天;使用 TemporalAdjusters.firstDayOfYear() 得到当前年的第一天;使用 TemporalAdjusters.previous(DayOfWeek.SATURDAY) 得到上一个周六;使用 TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY) 得到本月最后一个周五。

System.out.println("//本月的第一天"); System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth())); System.out.println("//今年的程序员日"); System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).plusDays(255)); System.out.println("//今天之前的一个周六"); System.out.println(LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.SATURDAY))); System.out.println("//本月最后一个工作日"); System.out.println(LocalDate.now().with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));可以直接使用 lanbda 表达式进行自定义的时间调整 System.out.println(LocalDate.now().with(temporal -> temporal.plus(ThreadLocalRandom.current().nextInt(100), ChronoUnit.DAYS)));除了计算外,还可以判断日期是否符合某个条件。

比如,自定义函数,判断指定日期是否是家庭成员的生日:public class DateTimeTest { private static LocalDateTime localDateTime = LocalDateTime.now(); public static void main(String[] args) { System.out.println(isFamilyBirthday(localDateTime)); } public static Boolean isFamilyBirthday(LocalDateTime date) { int month = date.getMonthValue(); int day = date.getDayOfMonth(); if (month == Month.JULY.getValue() && day == 10) return Boolean.TRUE; if (month == Month.SEPTEMBER.getValue() && day == 21) return Boolean.TRUE; if (month == Month.MAY.getValue() && day == 22) return Boolean.TRUE; return Boolean.FALSE; }}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/tougao/652340.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-17
下一篇 2022-04-17

发表评论

登录后才能评论

评论列表(0条)

保存