
To convert timezones, you first have to convert into the a ZonedDateTime, which accepts date, time and timezone. for example only represents a date and time without a specific timezone context and therefore can be used to parse your string value directly. Instant utcTimestamp = systemZoneDateTime.toInstant() Īs you can see from the names alone there are different classes for different use-cases of dates. timestamp of the original value represented in UTC ZonedDateTime utcDateTime = systemZoneDateTime.withZoneSameInstant(ZoneId.of("UTC")) value converted to other timezone while keeping the point in time ZonedDateTime systemZoneDateTime = localDateTime.atZone(ZoneId.systemDefault()) local date time at your system's default time zone LocalDateTime localDateTime = LocalDateTime.parse(dbTime, DateTimeFormatter.ofPattern(DATE_FORMAT))

parsed date time without timezone information String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss" On Java 8 and later I'd suggest to stick with the better designed java.time.* classes.

#JAVA CONVERT STRING TO DATE OBJECT FULL#
While it looks like a full date from the outside, it actually only represents a timestamp without storing actual timezone information. I have time in string format and I have to convert it into different formats mainly UTC to PST or PST to UTC.Īfter some research, I found this tutorial but was unable to get the expected output. Now I'm confused as to which is the correct approach to convert the time. This will convert the time into UTC instead of just creating a date object. String sDateInAmerica = sdfAmerica.format(date) // Convert to String firstĭateInAmerica = formatter.parse(sDateInAmerica) // Create a new Date object TimeZone utcTimeZone = TimeZone.getTimeZone("UTC") Now I wanted to initialize/create a Date object of UTC timezone.įor this, I have tried below code SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT) String dbTime = " 12:30:00" įinal String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss"

I have a date string with the given format. I'm learning Java and come across this issue.
