public classOffsetDateTimeTool {//private static final ZoneOffset OFFSET_ID = ZoneOffset.of("+08:00");
private static final ZoneOffset OFFSET_ID =getOFFSET_ID();/*** String 转 OffsetDateTime*/
public staticString getZoneStringDateTime(CharSequence text){returnformatDateTime(OffsetDateTime.of(LocalDateTimeTool.parseTime(text.toString()),OFFSET_ID)
,TimeFormat.LONG_DATE_PATTERN_WITH_TIME_ZONE.value());
}/*** String 转 OffsetDateTime 转 long 转 OffsetDateTime 格式化 String*/
public staticString getStringDateTime(CharSequence text) {returnformatDateTime(OffsetDateTime.ofInstant(Instant.ofEpochMilli(getLongDateTime(text
,TimeFormat.LONG_DATE_PATTERN_WITH_TIME_ZONE.value(),getOffset(text.toString()))),OFFSET_ID)
,TimeFormat.LONG_DATE_PATTERN_LINE.value());
}/*** String 转 OffsetDateTime 转 long*/
public static longgetLongDateTime(CharSequence text, String patten, String offsetId) {return getLong(parse(text,patten,offsetId))*1000;
}/*** OffsetDateTime 转 long*/
public static longgetLong(OffsetDateTime offsetDateTime) {returnoffsetDateTime.getLong(ChronoField.INSTANT_SECONDS);
}/*** 根据字符串设置日期
*
*@returnOffsetDateTime*/
public staticOffsetDateTime parse(CharSequence text, String patten, String offsetId) {
LocalDateTime localDateTime=LocalDateTime.parse(text, DateTimeFormatter.ofPattern(patten));returnOffsetDateTime.of(localDateTime, ZoneOffset.of(offsetId));
}/*** 格式化时间*/
public staticString formatDateTime(OffsetDateTime dateTime, String patten) {returndateTime.format(DateTimeFormatter.ofPattern(patten));
}/*** 获取系统时区*/
public staticZoneOffset getOFFSET_ID(){returnLocalDateTime.now(ZoneOffset.systemDefault()).atZone(ZoneOffset.systemDefault()).getOffset();
}/*** 获取string时区*/
public staticString getOffset(String date){return date.substring(date.length()-6,date.length());
}/*** 获取当前时间 在ISO-8601日历系统中的UTC /格林威治偏移的日期时间,如2017-12-03T10:15:30+08:00*/
public staticOffsetDateTime getDateTimeNow() {returnOffsetDateTime.now();
}/*** 获取时间(只设置年月日)*/
public static OffsetDateTime getDateTime(int year, int month, intday , String offsetId) {return getDateTime(year, month, day, 0, 0, 0, 0, ZoneOffset.of(offsetId));
}/*** 获取时间
*
*@paramyear
* 年
*@parammonth
* 月
*@paramday
* 日
*@paramhour
* 时
*@paramminute
* 分
*@paramsecond
* 秒
*@paramnanoOfSecond
* 纳秒
*@paramzoneOffset
* 时区
*@returnOffsetDateTime*/
public static OffsetDateTime getDateTime(int year, int month, int day, int hour, int minute, intsecond,intnanoOfSecond, ZoneOffset zoneOffset) {returnOffsetDateTime.of(year, month, day, hour, minute, second, nanoOfSecond, zoneOffset);
}/*** 内部枚举类*/
public enumTimeFormat {//短时间格式 年月日
/***时间格式:yyyy-MM-dd HH:mm:ss*/LONG_DATE_PATTERN_LINE("yyyy-MM-dd HH:mm:ss"),
LONG_DATE_PATTERN_WITH_TIME_ZONE("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");private String value = "";
TimeFormat(String value) {this.value =value;
}publicString value() {return this.value;
}
}public static voidmain(String args[]) {
System.out.println("getLongDateTime : "
+ getLongDateTime("2018-06-05T08:00:00.000+08:00",TimeFormat.LONG_DATE_PATTERN_WITH_TIME_ZONE.value(),getOffset("2018-06-05T04:00:00.000+08:00") ));
System.out.println("getLongDateTime : "
+ getLongDateTime("2018-06-05T04:00:00.000+04:00",TimeFormat.LONG_DATE_PATTERN_WITH_TIME_ZONE.value(),getOffset("2018-06-05T04:00:00.000+04:00") ));
System.out.println("getStringDateTime :" + getStringDateTime("2018-06-05T04:00:00.000-12:00"));
System.out.println("getZoneStringDateTime:" + getZoneStringDateTime("2018-06-05 04:00:00"));
}
}