从纪元到“mm / dd / yy”将毫秒转换时间

时间:2014-09-11 13:26:43

标签: java date jodatime

我将日期字符串转换为millis,就像这样

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是我想要转换的日期。

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

4 个答案:

答案 0 :(得分:2)

如果我理解您的问题,您可以使用DateTimeFormatter.print(long)

DateTimeFormatter shortFormat =
    DateTimeFormat.forPattern("MM/dd/yy").withZone(PST);
String formatted = shortFormat.print(millis);

来自链接的Javadoc,

  

将一个毫秒的瞬间打印到String。

答案 1 :(得分:2)

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

第一种方法是从日期String到Millis(Epoch)

    //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();     
   }

第二种方法是从Millis到Date String

    //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()方法的这些输入:

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

您应该获得以下输出:

1426575600000

2015年3月17日

希望这有帮助! ;)

答案 2 :(得分:1)

使用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;
    }
}

答案 3 :(得分:0)

只需构建一个新的DateTime对象即可。

DateTime newDate = new DateTime(millis);

Overloaded with timezone