Yogesh Srivastava
Yogesh Srivastava

Reputation: 311

Converting String hours and minutes in to UTC in java

I have hours and minutes in string and i want to convert it into UTC time zone below is my code but I am getting wrong hours and minutes please help me....thanks

    Calendar time = Calendar.getInstance(TimeZone.getTimeZone(Utils.merchantTimeZone));
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
    System.out.println("Raw time====" + rawTime);
    time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(rawTime.substring(0, 2)));
    time.set(Calendar.MINUTE, Integer.parseInt(rawTime.substring(2,4)));

    sdf.format(time);
    rawTime = time.get(Calendar.HOUR) +""+ time.get(Calendar.MINUTE);

Upvotes: 0

Views: 1606

Answers (2)

AuroMetal
AuroMetal

Reputation: 956

I'd suggest that you add the following line to your code:

sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

As a resource, I'm looking to the following thread answer given by Affe: Click here

Edit: Class TimeZone documentation. Also, here is a very nice tutorial by Jenkov here

Edit 2: Note to the Reader: The TimeZone class is now legacy, supplanted by ZoneId and ZoneOffset. Thanks to Basil Bourque

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338730

Calendar time = Calendar.getInstance(TimeZone.getTimeZone(Utils.merchantTimeZone));

TimeZone was replaced by ZoneId.

ZoneId z = ZoneId.of( "America/Edmonton" ) ; // Or `Africa/Tunis`, `Europe/Paris`, etc.

The Calendar class was replaced by ZonedDateTime. Capture the current moment by calling now.

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");

Better to automatically localize. To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine:
    • The human language for translation of name of day, name of month, and such.
    • The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Code:

FormatStyle style = FormatStyle.LONG ; 
Locale locale = new Locale( "en" , "IN" ) ;  // English in India.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( style ).withLocale( locale ) ;
String output = zdt.format( f ) ;

26 July 2019 at 7:28:36 PM GMT-06:00

If you would rather hard-code a formatting pattern, search StackOverflow for DateTimeFormatter.ofPattern. This has been covered many times already.

rawTime = time.get(Calendar.HOUR) +""+ time.get(Calendar.MINUTE);

If you want just the time-of-day portion without the date and without the time zone, extract a LocalTime.

LocalTime lt = zdt.toLocalTime() ;

If you want only the hour and minutes without seconds and fractional second, truncate.

LocalTime lt = zdt.toLocalTime().truncatedTo( ChronoUnit.MINUTES ) ;

i want to convert it into UTC time zone

This part of your Question is unclear. If you want to adjust from a zoned moment to see that same moment in UTC, simply convert to a OffsetDateTime object and adjust into UTC using the ZoneOffset.UTC constant.

OffsetDateTime odt = zdt.toOffsetDateTime() ;
OffsetDateTime odtUtc = odt.withOffsetSameInstant( ZoneOffset.UTC ) ;

What's the difference between a ZonedDateTime with a time zone and an OffsetDateTime with an offset-from-UTC? An offset is merely a number of hours-minutes-seconds, nothing more. A time zone is much more. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region.

When I am passing 0830(Asia/Calcutta) to rawTime then I got 1400 which is not a proper UTC Hour

Apparently you want to specify a time-of-day to a date.

First get today's date, as an example.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
LocalDate ld = LocalDate.now( z ) ;           // Current date as seen in India right now.

Specify your time-of-day.

LocalTime lt = LocalTime.of( 8 , 30 ) ;

Combine all three parts to get a ZonedDateTime. If that time-of-day is not valid on that date in that zone, ZonedDateTime will adjust.

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

zdt.toString(): 2019-07-27T08:30+05:30[Asia/Kolkata]

To see that same moment in UTC, extract a Instant. The Instant class is always in UTC, by definition.

Instant instant = zdt.toInstant() ;

instant.toString(): 2019-07-27T03:00:00Z

Notice the time-of-day. On this date, India is five and a half hours ahead of UTC. So 5.5 hours less on the time-of-day is 3 AM UTC.

is it possible to convert 23:00 (Asia/Calcutta) to UTC hours

Yes, similar to code just above. Here we alternatively call ZonedDateTime::with.

ZonedDateTime
.now( 
    ZoneId.of( "Asia/Kolkata" ) 
)
.with( 
    LocalTime.of( 23 , 0 ) 
)
.toInstant() 
.toString()

2019-07-27T17:30:00Z

Again, on this date India is five and a half hours ahead of UTC. So winding the face of a clock backwards 5.5 hours from 11 PM shows 5:30 PM.

diagram of date-time types in Java (both legacy & modern) and in standard SQL


The classes seen here are built into Java 8 and later, as well as Android 26 and later.

Upvotes: 2

Related Questions