Convert time in milliseconds from an era to "mm / dd / yy"

I convert 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 change it to get the date in PST when I have millis?

+6
source share
4 answers

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

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

From the related Javadoc,

Prints a millisecond moment for a string.

+2
source

I created 2 methods that are flexible enough to handle any date format in any time zone.

The first method is from the 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(); } 

The second method is from the string Millis to Date

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

+2
source

Java 8 solution using java.time api that converts the given string in millisecond and millisecond into date string taking into account the 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; } } 
+1
source

Just creating a new DateTime object should work.

 DateTime newDate = new DateTime(millis); 

Time Zone Overloaded

0
source

Source: https://habr.com/ru/post/975134/


All Articles