Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time in millis since epoch to "mm/dd/yy"

I'm converting a date string to millis like this

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

where startDate is the date I want to convert.

How do I reverse engineer this to get the date in PST when I have the millis ?

like image 530
Shan Avatar asked Sep 11 '14 13:09

Shan


3 Answers

If I understand your question you could use DateTimeFormatter.print(long),

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

From the linked Javadoc,

Prints a millisecond instant to a String.

like image 58
Elliott Frisch Avatar answered Sep 20 '22 18:09

Elliott Frisch


I created 2 methods that is flexible enough, to handle any date Format on any timezone.

First Method is from date String to 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();     
   }

Second Method is from Millis to 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);
    }

Try these inputs for your main() method:

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

You should get the following output:

1426575600000

2015-03-17

Hope this helps! ;)

like image 30
javaMaster Avatar answered Sep 21 '22 18:09

javaMaster


A Java 8 solution using java.time api which will convert a given string to millisecond and millisecond to string date considering Time Zone:

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;
    }
}
like image 30
Tapas Bose Avatar answered Sep 17 '22 18:09

Tapas Bose