前言
java 8的时间已经能够满足日常的使用,也方便理解。joda-time作为一个有优秀的时间组件也不得不告知使用者在java 8以后使用自带的时间
LocalDateTime以及ZonedDateTime使用
这两个时间的formate方法是返回DateTimeFormatter后的String
静态方法parse是返回对应的类型
例子如下
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j
public class Java8DateTimeTest {
/**
* 日期格式转换yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX TO yyyy-MM-dd HH:mm:ss
*
* @throws ParseException
*/
//public static DateTimeFormatter utcformat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public static DateTimeFormatter utcformat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//public static DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
public static DateTimeFormatter generalformat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
String utctimeStr = "2017-06-15T10:02:02.000+08:00";
String generalTime = "2017-06-15 00:02:02";
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
//时区时间变为iso8061时间
log.info("generalTime time {} convert jdk8 time {}", generalTime, LocalDateTime.parse(generalTime, generalformat).toString());
//iso8061时间转变为携带上海时区的时间
LocalDateTime localDateTime = LocalDateTime.parse(generalTime, generalformat);
//2017-06-15T00:02:02
log.info("localDateTime {}", localDateTime);
//2019-11-19T16:31:31.758+0800
ZonedDateTime zonedDateTime1 = localDateTime.atZone(shanghaiZoneId);
//2017-06-15T00:02:02.000+08:00
log.info("##zonedDateTime1 {}", zonedDateTime1.format(utcformat2));
//携带时区时间转变为 yyyy-MM-dd HH:mm:ss格式
//2017-06-15T10:02:02+08:00
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse(utctimeStr);
//###2017-06-15 10:02:02
log.info("##zonedDateTime2 to generalformat {}", zonedDateTime2.format(generalformat));
}
}
LocalDate
LocalDate是用作年月日的使用很方便
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Data
@Slf4j
public class Java8LocalDateTest implements Serializable {
private static final long serialVersionUID = 8589835559483231840L;
private String date;
//get set..
//因为格式是yymmdd所以只能是LocalDate,否则会抛出异常
public static LocalDate parseStr2LocalDate(String datestr, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDate localDate;
localDate = LocalDate.parse(datestr, dateTimeFormatter);
return localDate;
}
public static void main(String[] args) {
List<Java8LocalDateTest> list = new ArrayList<>();
Java8LocalDateTest on1 = new Java8LocalDateTest();
on1.setDate("2018-05-17");
Java8LocalDateTest on2 = new Java8LocalDateTest();
on2.setDate("2018-05-16");
Java8LocalDateTest on3 = new Java8LocalDateTest();
on3.setDate("2018-05-18");
Java8LocalDateTest on4 = new Java8LocalDateTest();
on4.setDate("2018-05-15");
list.add(on1);
list.add(on2);
list.add(on3);
list.add(on4);
//升序与降序
/*steam()是Collections方法,返回的是Stream对象流*/
/*Stream<T> sorted(Comparator<? super T> comparator)
返回由该流的元素组成的流,根据提供的Comparator进行排序。
对于有序流,排序稳定。 对于无序的流,不能保证稳定性。*/
/* 以下将将字符串累加到ArrayList中:
List<String> asList = stringStream.collect(Collectors.toList());*/
List<Java8LocalDateTest> collect = list.stream().sorted(new Comparator<Java8LocalDateTest>() {
@Override
public int compare(Java8LocalDateTest o1, Java8LocalDateTest o2) {
LocalDate d1 = parseStr2LocalDate(o1.getDate(), "yyyy-MM-dd");
LocalDate d2 = parseStr2LocalDate(o2.getDate(), "yyyy-MM-dd");
//return d1.compareTo(d2);升序
return d2.compareTo(d1);
}
}).collect(Collectors.toList());
for (Java8LocalDateTest java8LocalDateTest : collect) {
log.info("object = {}", java8LocalDateTest.toString());
}
}
}
/*输出
object = Java8LocalDateTest(date=2018-05-18)
object = Java8LocalDateTest(date=2018-05-17)
object = Java8LocalDateTest(date=2018-05-16)
object = Java8LocalDateTest(date=2018-05-15)
*/