java offsetdatetime_OffsetDateTime工具类

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"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值