JodaTime
joda:網址
maven座標
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.6</version>
</dependency>
示例 :方法使用
public class JodaTimeTest1 {
public static void main(String[] args) {
DateTime today = new DateTime();
DateTime time = today.plusDays(1);
System.out.println(today.toString("yyyy-MM-dd"));
System.out.println(time.toString("yyyy-MM-dd"));
System.out.println("===========");
DateTime dateTime = today.withDayOfMonth(1);
System.out.println(dateTime.toString("yyyy-MM-dd"));
System.out.println("===========");
LocalDate localDate = new LocalDate();
System.out.println(localDate);
System.out.println("===========");
LocalDate date = localDate.plusMonths(3).dayOfMonth().withMinimumValue();
System.out.println(date);
DateTime dateTime1 = new DateTime();
DateTime dt = dateTime1.minusYears(2).monthOfYear().setCopy(3).dayOfMonth().withMaximumValue();
System.out.println(dt.toString("yyyy-MM-dd"));
}
}
示例 :使用jodaTime轉換時間類型
public class JodaTimeTest2 {
public static Date converrUTC2Date(String utcDate) {
try {
DateTime dateTime = DateTime.parse(utcDate, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
return dateTime.toDate();
} catch (Exception ex) {
return null;
}
}
public static String convertDate2UTC(Date javaDate) {
DateTime dateTime = new DateTime(javaDate, DateTimeZone.UTC);
return dateTime.toString();
}
public static String convertDate2LocalByDateFormat(Date javaDate, String dataFormat) {
DateTime dateTime = new DateTime(javaDate);
return dateTime.toString(dataFormat);
}
public static void main(String[] args) {
System.out.println(JodaTimeTest2.converrUTC2Date("2014-11-04T09:22:54.876Z"));
System.out.println(JodaTimeTest2.convertDate2UTC(new Date()));
System.out.println(JodaTimeTest2.convertDate2LocalByDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
}
Java8 時間新特性API
示例:日常方法使用
public class Java8TimeTest {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("當前時間= " + localDate);
System.out.println(localDate.getYear() + " ," + localDate.getMonthValue() + " ," + localDate.getDayOfMonth());
System.out.println("------------");
LocalDate localDate1 = LocalDate.of(2020, 6, 18);
System.out.println(localDate1);
System.out.println("------------");
LocalDate localDate2 = LocalDate.of(2021, 3, 28);
MonthDay monthDay = MonthDay.of(localDate2.getMonth(), localDate2.getDayOfMonth());
MonthDay monthDay2 = MonthDay.from(LocalDate.of(2021, 3, 28));
System.out.println(monthDay.equals(monthDay2));
System.out.println("------------");
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalTime time = LocalTime.now().plusHours(2).plusMinutes(20);
System.out.println(time);
System.out.println("------------");
LocalDate localDate3 = localDate.plus(2, ChronoUnit.WEEKS);
System.out.println(localDate3);
System.out.println("------------");
LocalDate localDate4 = localDate.minus(2, ChronoUnit.MONTHS);
System.out.println(localDate4);
System.out.println("------------");
Clock clock = Clock.systemDefaultZone();
System.out.println(clock);
System.out.println(clock.millis());
System.out.println("--------------");
LocalDate localDate5 = LocalDate.now();
LocalDate localDate6 = LocalDate.of(2020, 6, 17);
System.out.println(localDate5.isBefore(localDate6));
System.out.println(localDate5.isAfter(localDate6));
System.out.println(localDate5.isEqual(localDate6));
System.out.println("-------------------------");
Set<String> ids = ZoneId.getAvailableZoneIds();
Set<String> stringSet = new TreeSet<String>();
stringSet.addAll(ids);
stringSet.forEach(System.out::println);
System.out.println("---------------------");
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
System.out.println(zonedDateTime);
System.out.println("---------------------");
YearMonth yearMonth = YearMonth.now();
System.out.println(yearMonth.lengthOfMonth());
System.out.println(yearMonth.isLeapYear());
System.out.println("--------------");
YearMonth yearMonth1 = YearMonth.of(2020, 6);
System.out.println(yearMonth1);
System.out.println(yearMonth.lengthOfYear());
System.out.println(yearMonth.lengthOfMonth());
System.out.println("----------------");
LocalDate localDate7 = LocalDate.now();
LocalDate localDate8 = LocalDate.of(2040, 3, 18);
Period period = Period.between(localDate8, localDate7);
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());
System.out.println("----------------");
System.out.println(Instant.now());
}
}