Home > database >  How do I convert this time format into epoch in java? "Tue Dec 28 16:55:00 GMT 05:30 2021"
How do I convert this time format into epoch in java? "Tue Dec 28 16:55:00 GMT 05:30 2021"

Time:12-22

I am using 'Single Date and Time Picker' Library in my Android Project but it only returns date and time in the below mentioned format.

"Tue Dec 28 16:55:00 GMT 05:30 2021"

I want to convert this into epoch time format.

Library: https://github.com/florent37/SingleDateAndTimePicker

CodePudding user response:

Alternative solution with java.time.OffsetDateTime

Here's an alternative solution that makes use of java.time keeping all the information of the input String:

public static void main(String[] args) throws IOException {
    // input
    String dpDate = "Tue Dec 28 16:55:00 GMT 05:30 2021";
    // define a formatter with the pattern and locale of the input
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
            "EEE MMM dd HH:mm:ss OOOO uuuu", Locale.ENGLISH);
    // parse the input to an OffsetDateTime using the formatter
    OffsetDateTime odt = OffsetDateTime.parse(dpDate, dtf);
    // receive the moment in time represented by the OffsetDateTime
    Instant instant = odt.toInstant();
    // extract its epoch millis
    long epochMillis = instant.toEpochMilli();
    // and the epoch seconds
    long epochSeconds = instant.getEpochSecond();
    // and print all the values
    System.out.println(String.format("%s ---> %d (ms), %d (s)",
                                        odt, epochMillis, epochSeconds));
}

Output:

2021-12-28T16:55 05:30 ---> 1640690700000 (ms), 1640690700 (s)

A LocalDateTime should not be used here, because you may lose the information about the offset and a ZonedDateTime can neither be used due to the input lacking information about a zone like "Asia/Kolkata" or "America/Chicago", it just provides an offset from UTC.

If you simply want to get the epoch millis, you can write a short method:

// define a constant formatter in the desired class
private static final DateTimeFormatter DTF_INPUT =
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
            "EEE MMM dd HH:mm:ss OOOO uuuu", Locale.ENGLISH);

…

/**
 * parses the input, converts to an instant and returns the millis
 */
public static long getEpochMillisFrom(String input) {
    return OffsetDateTime.parse(input, DTF_INPUT)
                         .toInstant()
                         .toEpochMilli();
}

CodePudding user response:

The format for your date is EEE MMM dd HH:mm:ss zzzz yyyy

String date = "Tue Dec 28 16:55:00 GMT 05:30 2021";
try {
    val sdf = SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy")
    val mDate = sdf.parse(date)
    val epochTime = TimeUnit.MILLISECONDS.toSeconds(mDate.time)
} catch (e: ParseException) {
    e.printStackTrace()
}
   

Variable epochTime will have the seconds stored in it.
To convert it back to a format you can do -

val sdf = SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy")
sdf.format(epochTime)

Latest Java 8 Requires Min Api Level 26

    String date = "Tue Dec 28 16:55:00 GMT 05:30 2021";
    DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
    LocalDateTime parsedDate = LocalDateTime.parse(date, format);
    val milliSeconds = parsedDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

CodePudding user response:

By Following Ansari's Answer, I did this to convert it into Epoch

SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);

Date d1 = null;
try{
d1 = sdf3.parse("Tue Dec 28 16:55:00 GMT 05:30 2021");
epochTime = TimeUnit.MILLISECONDS.toSeconds(d1.getTime());
Log.e("epoch time", "onDateSelected: " epochTime );
}
catch (Exception e){ e.printStackTrace(); }
  • Related