简体   繁体   English

将字符串日期转换为纪元时间的意外值?

[英]Unexpected value converting String date into epoch time?

The below gives: 1475020875000 . 下面给出: 1475020875000 When I convert this epoch back to a human readable timestamp, I get: Wed, 28 Sep 2016 00:01:15 GMT , which is different from the initial date? 当我将此时间段转换回人类可读的时间戳时,我得到: Wed, 28 Sep 2016 00:01:15 GMT ,与初始日期不同吗?

String date = "2016-09-27 20:01:15.0";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long epoch = df.parse(date).getTime();

System.out.println(epoch);

You should specify Timezone for both input and output. 您应该为输入和输出都指定时区。 You can use "z" to instantiate SimpleDateFormat and setTimeZone before using format method: 您可以在使用格式方法之前使用“ z”实例化SimpleDateFormat和setTimeZone:

package stackoverflow;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Programa {

    public static void main(String[] args) throws ParseException {
        String date = "2016-09-27 20:01:15 GMT";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        long epoch = df.parse(date).getTime();

        System.out.println(epoch);

        Date d = new Date(epoch);
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        String out = df.format(d);
        System.out.println(out);
    }

}

For available Timezones, try TimeZone.getAvailableIDs() 有关可用的时区,请尝试TimeZone.getAvailableIDs()

Worked fine here: 在这里工作正常:

package stackoverflow;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Programa {

    public static void main(String[] args) throws ParseException {
        String date = "2016-09-27 20:01:15.0";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long epoch = df.parse(date).getTime();

        System.out.println(epoch);

        Date d = new Date(epoch);
        String out = df.format(d);
        System.out.println(out);
    }

}

You certainly forgot to consider the Timezone to use. 您当然忘了考虑使用时区。 If you do not define it, the default (from the JVM) is taken, and can for example differ depending on your server. 如果未定义,则采用默认值(来自JVM),例如,默认值可能因服务器而异。

Try LocalDateTime 试试LocalDateTime

    String date = "2016-09-27 20:01:15";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long epoch = df.parse(date).getTime();
    System.out.println(epoch);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    LocalDateTime ld = LocalDateTime.parse(date, formatter);
    long epoch2 = ld.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    System.out.println(epoch2);
    Instant in = Instant.ofEpochMilli(epoch2);
    LocalDateTime ldt = LocalDateTime.ofInstant(in, ZoneId.systemDefault());
    System.out.println(ldt);

output 输出

1474986675000
1474986675000
2016-09-27T20:01:15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM