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 ?
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.
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! ;)
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;
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With