0

I am trying to convert a UTC string to just the hours and the minutes. I get the UTC string from an API but have given an example below of what it looks like.

When it gets to someDate it throws an Unparseable Date error and references the string setString.

Can anyone see what I am doing wrong here?

Example of how I am getting the date from UTC

String utcStr = "1521698232";
Date setSunrise = new Date(Long.parseLong(sunrise)*1000);

Trying to convert it to HH:mm

String setString = "Thu Mar 22 05:57:06 GMT+00:00 2018";

Date someDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z'+00:00' yyyy").parse(setString);

Date printDate = new SimpleDateFormat("hh:mm").format(someDate);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
JoeBoggs
  • 281
  • 1
  • 5
  • 12
  • 1
    have you tried removing '+00:00' from your format "EEE MMM d HH:mm:ss z'+00:00' yyyy"? Here they mention z as a format of GMT+00:00 already: https://developer.android.com/reference/java/text/SimpleDateFormat.html – TheWhiteLlama Mar 22 '18 at 19:56
  • 1
    Must've missed that! Thanks – JoeBoggs Mar 22 '18 at 19:59
  • Possible duplicate of [How to convert Date.toString back to Date?](https://stackoverflow.com/questions/9431927/how-to-convert-date-tostring-back-to-date) – Ole V.V. Mar 22 '18 at 20:34

2 Answers2

1

tl;dr

You are working too hard, going in a roundabout manner. Also, you are using troublesome old obsolete classes. Also, I suspect you are ignoring the crucial issue of time zone.

Here is a much simpler and cleaner modern solution, with consideration for time zone.

Instant.ofEpochSecond(                 // Represent a moment in time in UTC, with a resolution of nanoseconds.
    Long.parseLong( "1521698232" )     // Count of whole seconds since epoch of 1970-01-01T00:00:Z.
)                                      // Returns a `Instant` object.
.atZone(                               // Apply a time zone (`ZoneId`) to adjust from UTC to the wall-clock time of the target audience. 
    ZoneId.of( "Asia/Kolkata" )        // Use only proper time zone names `continent/region`. Never use 3-4 letter codes such as `IST` or `EST`. 
)                                      // Produces a `ZonedDateTime` object.
.toLocalTime()                         // Extract only the time-of-day as a `LocalTime` object.
.truncatedTo( ChronoUnit.MINUTES )     // Lop off any seconds and fractional second.
.toString()                            // Generate a String in standard ISO 8601 format: HH:MM:SS.SSSSSSSSS

11:27

Count-from-epoch

convert a UTC string

No such thing as a “UTC string”.

Your input seems to represent a number of whole seconds since the epoch reference of first moment of 1970 UTC, 1970-01-01T00:00Z. This is sometimes referred to as Unix Time or POSIX Time.

ISO 8601

"Thu Mar 22 05:57:06 GMT+00:00 2018";

This is a terrible format for a date-time value.

Instead use standard ISO 8601 strings when exchanging date-time values as text. The java.time classes use ISO 8601 formats by default when parsing/generating strings.

Avoid legacy date-time classes

The Date and SimpleDateFormat classes are part of the troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Date is replaced by Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

String input = "1521698232" ;  // Count of seconds since epoch reference of 1970-01-01T00:00Z.
long secondsSinceEpoch = Long.parseLong( input ) ;
Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ;

instant.toString(): 2018-03-22T05:57:12Z

As discussed above, the Instant (like Date) is in UTC. If you ask for the time-of-day, you'll get a time-of-day in UTC. More likely you really want the time-of-day for that moment by the wall-clock time used by people in a certain region (a time zone).

A time zone is crucial in determining a date and time-of-day. For any given moment, the date and time-of-day varies around the globe by zone.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter pseudo-zones such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;

Apply that zone to adjust from UTC, producing a ZonedDateTime object.

ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString(): 2018-03-22T18:57:12+13:00[Pacific/Auckland]

Now ask for the time-of-day. The resulting LocalTime objects lacks a date and lacks a time zone. It is just a time-of-day on a 24-hour clock.

LocalTime lt = zdt.toLocalTime() ;

If you only care about the hours and minutes, lop off and seconds and fractional second by truncating. Specify the level of truncation via the ChronoUnit class.

LocalTime ltTrunc = lt.truncatedTo( ChronoUnit.MINUTES ) ;

Generate a String in standard ISO 8601 format.

String output = ltTrunc.toString() ;  // Generate a `String` in standard ISO 8601 format.

18:57

To generate a String in other formats, search Stack Overflow for DateTimeFormatter. You will find many discussions and examples.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

The +00:00 part is a UTC offset, and you can't treat as a literal (inside quotes, like you did). That's an important information, because it tells you how many hours ahead or behind UTC the date refers to (in this case, it's zero, so it's the same as UTC itself).

Another detail is that the day-of-week and month name are in English, so you should set a java.util.Locale in your class. If you don't use a locale, it'll use the JVM default and there's no guarantee that it'll always be English in all environments. If you're sure about the language used in the inputs, set the locale:

String setString = "Thu Mar 22 05:57:06 GMT+00:00 2018";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.ENGLISH);
Date someDate = parser.parse(setString);

For the output, 2 things:

  • using hh will print the hour-of-am-pm, which means values from 1 to 12. If you want the hours value from 0 to 23, use HH - this is all explained in the docs
  • the value of the hours will be converted to the device's default timezone, which means that not always will be the same of the input (in my case, my country is using -03:00 - 3 hours behind UTC - so the value of the hours is 2 AM.

To use the same offset in the input, you must set it in the formatter:

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
String printDate = formatter.format(someDate); // 05:57

To use java-time classes, the other answer by Basil tells you how to use this API in Android. I'd just like to add the similar code to parse your specific input:

String setString = "Thu Mar 22 05:57:06 GMT+00:00 2018";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss O yyyy", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse(setString, parser);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
String printDate = formatter.format(odt);
riens
  • 45
  • 3