Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将自历元起的时间(毫秒)转换为;年月日;_Java_Date_Jodatime - Fatal编程技术网

Java 将自历元起的时间(毫秒)转换为;年月日;

Java 将自历元起的时间(毫秒)转换为;年月日;,java,date,jodatime,Java,Date,Jodatime,我正在将一个日期字符串转换为毫秒,如下所示 import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; private static final DateTimeZone PST = DateTimeZone.forID("PST8PDT")

我正在将一个日期字符串转换为毫秒,如下所示

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


private static final DateTimeZone PST = DateTimeZone.forID("PST8PDT");
private static final DateTimeFormatter DATE_FORMATTER =
    DateTimeFormat.forPattern("yyyy-MM-dd").withZone(PST);


Long millis = DateTime.parse(startDate, DATE_FORMATTER).withTimeAtStartOfDay().getMillis());
其中startDate是我要转换的日期

当我有millis时,如何对其进行反向工程以获取PST中的日期?

简单地构造一个新对象就可以了

DateTime newDate = new DateTime(millis);

如果我理解你的问题,你可以用

从链接的Javadoc

将毫秒瞬间打印到字符串


使用
Java.time
api的Java 8解决方案,该解决方案将给定字符串转换为毫秒,毫秒转换为字符串日期(考虑时区):

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;


public class SO25788709 {

    public static void main(String[] args) {
        String strDate = "2014-09-12 23:59:59";
        String pattern = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        ZoneId zone = ZoneId.of("America/Los_Angeles");

        long milli = getMillis(strDate, formatter, zone);                       
        System.out.println(milli);

        String retStrDate = getDateString(milli, formatter, zone);
        System.out.println(retStrDate);
    }

    private static long getMillis(String strDate, DateTimeFormatter formatter, ZoneId zone) {
        LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone);
        Instant instant = zonedDateTime.toInstant();
        long milli = instant.toEpochMilli();
        return milli;
    }

    private static String getDateString(long milli, DateTimeFormatter formatter, ZoneId zone) {
        Instant instant = Instant.ofEpochMilli(milli);
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zone);
        String strDate = zonedDateTime.format(formatter);
        return strDate;
    }
}

我创建了两个足够灵活的方法,可以处理任何时区的任何日期格式

第一种方法是从日期字符串到毫秒(历元)

第二种方法是从毫秒到日期字符串

    //dateInMillis to date format yyyy-MM-dd
    private static String formatterMillistoDate(long dateInMillis, String format, String timeZone){
       //define your format
       DateTimeFormatter customFormat = DateTimeFormat.forPattern(format);
       //convert to DateTime with your desired TimeZone
       DateTime dateTime = new DateTime(dateInMillis, DateTimeZone.forID(timeZone));
       //return date String in format you defined
       return customFormat.print(dateTime);
    }
为main()方法尝试以下输入:

您应该获得以下输出:

1426575600000

2015-03-17


希望这有帮助!;)

我将如何用它指定时区?它可以用第二个参数timezone重载。我不确定PST的正确值是多少,但我相信您可以找到它。在帖子中链接。你应该使用“美国/洛杉矶”而不是“PST”或“PST8PDT”,因为Joda Time强烈喜欢第一种格式。时区名称及其缩写通常不是唯一的。PST也可能意味着巴基斯坦标准时间,而Joda Time在解析这些缩写时遇到了很大的问题。
    //dateString to long
    private static long formatterDateToMillis(String dateString, String format, String timeZone){   
       //define Timezone, in your case you hardcoded "PST8PDT" for PST
       DateTimeZone yourTimeZone = DateTimeZone.forID(timeZone);
       //define your pattern
       DateTimeFormatter customFormat = DateTimeFormat.forPattern(format).withZone(yourTimeZone);
       //parse dateString to the format you wanted
       DateTime dateTime = customFormat.parseDateTime(dateString);
       //return in Millis, usually in epoch
       return dateTime.getMillis();     
   }
    //dateInMillis to date format yyyy-MM-dd
    private static String formatterMillistoDate(long dateInMillis, String format, String timeZone){
       //define your format
       DateTimeFormatter customFormat = DateTimeFormat.forPattern(format);
       //convert to DateTime with your desired TimeZone
       DateTime dateTime = new DateTime(dateInMillis, DateTimeZone.forID(timeZone));
       //return date String in format you defined
       return customFormat.print(dateTime);
    }
    long valueInMillis = formatterDateToMillis("2015-03-17","yyyy-MM-dd","PST8PDT");
    System.out.println(valueInMillis);

    String formattedInDate = formatterMillistoDate(1426575600000L,"yyyy-MM-dd","PST8PDT");
    System.out.println(formattedInDate);