GVKun编程网logo

在Java中将纪元转换为ZonedDateTime(java分转换成元)

4

想了解在Java中将纪元转换为ZonedDateTime的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java分转换成元的相关问题,此外,我们还将为您介绍关于Java8–ConvertIn

想了解在Java中将纪元转换为ZonedDateTime的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java分转换成元的相关问题,此外,我们还将为您介绍关于Java 8 – Convert Instant to ZonedDateTime、java 8 – ZonedDateTime不等于另一个ZonedDateTime、Java 8 – 将LocalDate转换为ZonedDateTime、java 8时间使用LocalDateTime,ZonedDateTime,LocalDate的新知识。

本文目录一览:

在Java中将纪元转换为ZonedDateTime(java分转换成元)

在Java中将纪元转换为ZonedDateTime(java分转换成元)

如何在Java中将纪元转换1413225446.92000ZonedDateTime

给定的代码期望 值,因此将抛出NumberFormatException上面给出的值。

ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateInMillis)), ZoneId.of(TIME_ZONE_PST));

答案1

小编典典

罗勒·布尔克(BasilBourque)的答案很不错。从小数部分中取出纳秒到一个纳秒整数可能会导致一个或两个陷阱。我建议:

    String dateInMillis = "1413225446.92000";    String[] secondsAndFraction = dateInMillis.split("\\.");    int nanos = 0;    if (secondsAndFraction.length > 1) { // there’s a fractional part        // extend fractional part to 9 digits to obtain nanoseconds        String nanosecondsString                = (secondsAndFraction[1] + "000000000").substring(0, 9);        nanos = Integer.parseInt(nanosecondsString);        // if the double number was negative, the nanos must be too        if (dateInMillis.startsWith("-")) {            nanos = -nanos;        }     }    ZonedDateTime zdt = Instant            .ofEpochSecond(Long.parseLong(secondsAndFraction[0]), nanos)            .atZone(ZoneId.of("Asia/Manila"));    System.out.println(zdt);

此打印

2014-10-14T02:37:26.920+08:00[Asia/Manila]

纳秒级不需要64位,因此我只是使用int

假设:我假设您的字符串包含一个浮点数,并且可以对其进行签名,例如,-1.50这意味着在纪元开始
一秒半。如果某一天的时间用科学计数法(1.41322544692E9)来表示,则以上内容将无效。

如果不是亚洲/马尼拉,请以 _区域/城市_格式替换所需的时区,例如,美国/温哥华,美国/洛杉矶或太平洋/皮特凯恩。避免使用PST等三个字母缩写,因为它们是模棱两可的,通常不是真实的时区。

Java 8 – Convert Instant to ZonedDateTime

Java 8 – Convert Instant to ZonedDateTime

1. Instant -> ZonedDateTime

Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9

InstantZonedDateTime1.java
package com.mkyong.date;

import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; public class InstantZonedDateTime1 { public static void main(String[] argv) { // Z = UTC+0 Instant instant = Instant.now(); System.out.println("Instant : " + instant); // Japan = UTC+9 ZonedDateTime jpTime = instant.atZone(ZoneId.of("Asia/Tokyo")); System.out.println("ZonedDateTime : " + jpTime); System.out.println("OffSet : " + jpTime.getOffset()); } } 
 

Output

Instant : 2016-08-18T06:17:10.225Z
LocalDateTime : 2016-08-18T06:17:10.225

2. ZonedDateTime -> Instant

Convert the Japan ZonedDateTime UTC+9 back to a Instant UTC+0/Z, review the result, the offset is taken care automatically.

InstantZonedDateTime2.java
package com.mkyong.date;

import java.time.*; public class InstantZonedDateTime2 { public static void main(String[] argv) { LocalDateTime dateTime = LocalDateTime.of(2016, Month.AUGUST, 18, 6, 57, 38); // UTC+9 ZonedDateTime jpTime = dateTime.atZone(ZoneId.of("Asia/Tokyo")); System.out.println("ZonedDateTime : " + jpTime); // Convert to instant UTC+0/Z , java.time helps to reduce 9 hours Instant instant = jpTime.toInstant(); System.out.println("Instant : " + instant); } } 
 

Output

ZonedDateTime : 2016-08-18T06:57:38+09:00[Asia/Tokyo]
Instant : 2016-08-17T21:57:38Z

http://www.mkyong.com/java8/java-8-convert-instant-to-zoneddatetime/
http://www.mkyong.com/tutorials/java-date-time-tutorials/

java 8 – ZonedDateTime不等于另一个ZonedDateTime

java 8 – ZonedDateTime不等于另一个ZonedDateTime

我创建了两个zoneddatetime对象,我认为它们应该是相同的:
public static void main(String[] args) {
    ZoneId zid = ZoneId.of("America/New_York");
    ZoneOffset offset = ZoneOffset.from(LocalDateTime.Now().atZone(zid));
    zoneddatetime zdt0 = zoneddatetime.of(2014,8,24,21,10,1,777000002,offset);
    zoneddatetime zdt1 = zoneddatetime.of(2014,zid);
    boolean equals = Objects.equals(zdt0,zdt1);
    System.out.println("equals: " + equals);
}

在调试器中,我看到第一种情况下zoneddatetime区域的成员类是java.time.ZoneOffset,第二种情况是java.time.ZoneRegion,这使得zoneddatetime对象不相等.这令人困惑……
有任何想法吗?

解决方法

您正在检查对象相等性,其值为false,因为这些对象不相等.一个绑定到ZoneId,另一个绑定到ZoneOffset.如果要检查它们是否代表同一时间,则可以使用非常直观命名的方法isEqual.

例如.:

ZoneId zid = ZoneId.of("America/New_York");
ZoneOffset offset = ZoneOffset.from(LocalDateTime.Now().atZone(zid));
zoneddatetime zdt0 = zoneddatetime.of(2014,offset);
zoneddatetime zdt1 = zoneddatetime.of(2014,zid);
System.out.println("isEqual:" + zdt0.isEqual(zdt1));
System.out.println("equals: " + zdt0.equals(zdt1));

打印:

isEqual:true
equals: false

顺便说一下,请注意,您不需要为已知的两个对象使用Objects.equals(a,b)非空.您可以直接调用a.equals(b).

Java 8 – 将LocalDate转换为ZonedDateTime

Java 8 – 将LocalDate转换为ZonedDateTime

我是 java.time包的新手.我有一个LocalDate 2015-12-10.我需要将其转换为zoneddatetime.时间应为00:00:00,Zone为ZoneOffset.UTC.

转换后,zoneddatetime应为2015-12-10T00:00:00 02:00.

我将LocalDate存储在一个名为startDate的变量中.

我试过了:

zoneddatetime.ofInstant(Instant.from(startDate),ZoneOffset.UTC)

但是得到错误

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: 2015-12-10 of type java.time.LocalDate]

我也试过:

startDate.atStartOfDay().atZone(ZoneOffset.UTC)

这样可以预见到结果

我看了API,尝试了一些其他的方法,但迄今没有运气.

有没有其他办法将LocalDate转换为zoneddatetime?

解决方法

当将较不具体的对象转换为更具体的对象时,请使用“at”方法:
zoneddatetime zdt = startDate.atStartOfDay(ZoneOffset.UTC);

传递UTC偏移量将不会得到02:00的结果,这表明您正在尝试取得其他成果.

java 8时间使用LocalDateTime,ZonedDateTime,LocalDate

java 8时间使用LocalDateTime,ZonedDateTime,LocalDate

前言

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) 

*/

今天关于在Java中将纪元转换为ZonedDateTimejava分转换成元的分享就到这里,希望大家有所收获,若想了解更多关于Java 8 – Convert Instant to ZonedDateTime、java 8 – ZonedDateTime不等于另一个ZonedDateTime、Java 8 – 将LocalDate转换为ZonedDateTime、java 8时间使用LocalDateTime,ZonedDateTime,LocalDate等相关知识,可以在本站进行查询。

本文标签: