Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2351977
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2015-08-28 11:04:07


  1. package datetime;
  2.  
  3. import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
  4. import static java.time.temporal.TemporalAdjusters.previousOrSame;
  5.  
  6. import java.time.DayOfWeek;
  7. import java.time.Duration;
  8. import java.time.LocalDate;
  9. import java.time.LocalDateTime;
  10. import java.time.LocalTime;
  11. import java.time.Month;
  12. import java.time.OffsetTime;
  13. import java.time.Period;
  14. import java.time.ZoneId;
  15. import java.time.ZoneOffset;
  16. import java.time.ZonedDateTime;
  17. import java.time.temporal.ChronoUnit;
  18.  
  19. public class DataTimes {
  20.  
  21.     public static void main(String[] args) {
  22.         // 创建时间对象
  23.         LocalDateTime timePoint = LocalDateTime.now(); // 当前时间
  24.         System.out.println("--当前时间----");
  25.         System.out.println(timePoint);
  26.         System.out.println("");
  27.  
  28.         System.out.println("--自定义时间----");
  29.         System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
  30.                                                                     // values
  31.         System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
  32.         System.out.println(LocalTime.of(17, 18)); // the train I took home today
  33.         System.out.println(LocalTime.parse("10:15:30")); // From a String
  34.         System.out.println("");
  35.  
  36.         System.out.println("--获取时间的各个部分----");
  37.         System.out.println(timePoint.toLocalDate());
  38.         System.out.println(timePoint.getMonth());
  39.         System.out.println(timePoint.getDayOfMonth());
  40.         System.out.println(timePoint.getSecond());
  41.         System.out.println("");
  42.  
  43.         System.out.println("---设置并返回新的时间对象---");
  44.         LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
  45.         System.out.println(thePast);
  46.         System.out.println("---再加3周---");
  47.         LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
  48.                 ChronoUnit.WEEKS);
  49.         System.out.println(yetAnother);
  50.         System.out.println("");
  51.  
  52.         System.out.println("---使用时间调整函数---");
  53.         System.out.println(timePoint);
  54.         System.out.println(timePoint.with(lastDayOfMonth()));
  55.         System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
  56.         System.out.println(timePoint.with(LocalTime.now()));
  57.         System.out.println("");
  58.  
  59.         System.out.println("---截断时间精确位--");
  60.         System.out.println(timePoint);
  61.         LocalDateTime truncatedTimeToMinutes = timePoint
  62.                 .truncatedTo(ChronoUnit.MINUTES);
  63.         System.out.println(truncatedTimeToMinutes);
  64.         LocalDateTime truncatedTimeToSeconds = timePoint
  65.                 .truncatedTo(ChronoUnit.SECONDS);
  66.         System.out.println(truncatedTimeToSeconds);
  67.         System.out.println("");
  68.  
  69.         System.out.println("---使用时区---");
  70.         ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
  71.                 ZoneId.of("UTC+08:00"));
  72.         System.out
  73.                 .println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
  74.         System.out.println(zoned_now.getOffset());
  75.         System.out.println("");
  76.  
  77.         System.out.println("---时间上使用时区偏移---");
  78.         OffsetTime time = OffsetTime.now();
  79.         ZoneOffset offset = ZoneOffset.of("+02:00");
  80.         System.out.println(time);
  81.         System.out.println(time.withOffsetSameInstant(offset));
  82.         System.out.println("");
  83.  
  84.         System.out.println("---时间加减---");
  85.         timePoint = LocalDateTime.now();
  86.         System.out.println(timePoint);
  87.         // 3 years, 2 months, 1 day
  88.         Period period1 = Period.of(3, 2, 1);
  89.         System.out.println(timePoint.plus(period1));
  90.         Duration duration = Duration.ofSeconds(3, 5);
  91.         System.out.println(timePoint.plus(duration));
  92.         Duration sixHours = Duration.between(
  93.                 ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
  94.                 ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
  95.         System.out.println(timePoint.plus(sixHours));
  96.         System.out.println("");
  97.  
  98.     }
  99.  
  100. }
转自: />
阅读(1657) | 评论(0) | 转发(0) |
0

上一篇:java获取ResultSet记录行数

下一篇:没有了

给主人留下些什么吧!~~