11
  • this is my date " 15-05-2014 00:00:00 "

  • how to convert IST to UTC i.e( to 14-05-2014 18:30:00)

  • based on from timezone to UTC timezone.

my code is

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");

formatter.setTimeZone(TimeZone.getTimeZone("IST"));  //here set timezone

System.out.println(formatter.format(date));  
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));  //static UTC timezone

System.out.println(formatter.format(date));  
String str = formatter.format(date);
Date date1  = formatter.parse(str);
System.out.println(date1.toString());
  • if user enter same date from any zone then will get UTC time(ex: from Australia then 15-05-2014 00:00:00 to 14-05-2014 16:00:00)

  • please any suggestions.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
user3599212
  • 429
  • 2
  • 6
  • 18
  • 1
    when you print it will always be in the local timezone irrespective of IST or EST. so is your assignment related to only printing or you want to use that value further – vikeng21 Jun 16 '14 at 09:59
  • thanks for replay, different timezone users entered From and To dates, i want convert that date values and use for further calculations.i did not printing to this values. – user3599212 Jun 16 '14 at 10:29
  • 1
    possible duplicate of [Convert date string (EST) to Java Date (UTC)](http://stackoverflow.com/questions/12919067/convert-date-string-est-to-java-date-utc) – Basil Bourque Jun 17 '14 at 14:00
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 18 '18 at 20:30

2 Answers2

16

You cannot "convert that date values" to other timezones or UTC. The type java.util.Date does not have any internal timezone state and only refers to UTC by spec in a way which cannot be changed by user (just counting the milliseconds since UNIX epoch in UTC timezone leaving aside leapseconds).

But you can convert the formatted String-representation of a java.util.Date to another timezone. I prefer to use two different formatters, one per timezone (and pattern). I also prefer to use "Asia/Kolkata" in your case because then it will universally works (IST could also be "Israel Standard Time" which will be interpreted differently in Israel):

DateFormat formatterIST = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatterIST.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // better than using IST
Date date = formatterIST.parse("15-05-2014 00:00:00");
System.out.println(formatterIST.format(date)); // output: 15-05-2014 00:00:00

DateFormat formatterUTC = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatterUTC.setTimeZone(TimeZone.getTimeZone("UTC")); // UTC timezone
System.out.println(formatterUTC.format(date)); // output: 14-05-2014 18:30:00

// output in system timezone using pattern "EEE MMM dd HH:mm:ss zzz yyyy"
System.out.println(date.toString()); // output in my timezone: Wed May 14 20:30:00 CEST 2014
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
13

tl;dr

LocalDateTime.parse( 
    "15-05-2014 00:00:00" , 
    DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) 
)
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.toInstant()

java.time

The Answer by Meno Hochschild is correct but shows classes that are now outdated.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( "15-05-2014 00:00:00" , f ) ;

ldt.toString(): 2014-05-15T00:00

Apparently you are certain that string represents a moment in India time. Tip: You should have included the zone or offset in that string. Even better, use standard ISO 8601 formats.

Assign the India time zone.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;

zdt.toString(): 2014-05-15T00:00+05:30[Asia/Kolkata]

To see the same moment, the same point on the timeline, through the wall-clock time of UTC, extract an Instant.

Instant instant = zdt.toInstant() ;

instant.toString(): 2014-05-14T18:30:00Z


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.

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or 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