Setting time zone of a java util Date

0 votes

Can someone help me in specifying the time zone of a date object that I have parsed from a String?
The timezone is not specified in the String but by default, it is setting the local time zone as the time zone of the date object.
 Please suggest.

Aug 14, 2018 in Java by 93.lynn
• 1,620 points

3 answers to this question.

0 votes

You can make use of the following DateFormat.

SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date newDate = myDate.parse("2010-05-23T09:01:02");

You need to do this because by default java.util.Date objects do not contain any timezone information.

answered Aug 14, 2018 by anto.trigg4
• 3,440 points
0 votes

Be aware that java.util.Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.

As ZZ Coder shows, you set the timezone on the DateFormat object, to tell it in which timezone you want to display the date and time.

answered Dec 15, 2020 by Roshni
• 10,440 points
0 votes

Using Java 8

Java 8 introduced a new Date-Time API for working with dates and times which was largely based on the Joda-Time library.

The Instant class from Java Date Time API models a single instantaneous point on the timeline in UTC. This represents the count of nanoseconds since the epoch of the first moment of 1970 UTC.

First, we'll obtain the current Instant from the system clock and ZoneId for a time zone name:

Instant nowUtc =;
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");

Finally, the ZoneId and Instant can be utilized to create a date-time object with time-zone details. The ZonedDateTime class represents a date-time with a time-zone in the ISO-8601 calendar system:

ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);

We've used Java 8's ZonedDateTime to represent a date-time with a time zone.

answered Dec 15, 2020 by Gitika
• 65,870 points

Related Questions In Java

0 votes
2 answers

Converting between java.time.LocalDateTime and java.util.Date

Try this: Date in = new Date(); LocalDateTime ldt ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 59,050 points
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,510 points
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,050 points
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,640 points
0 votes
1 answer

The best way to filter a Java Collection?

 this problem is solved using streams and ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,630 points
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,890 points
0 votes
1 answer
0 votes
2 answers

Scanner is skipping nextLine() after using next() or nextFoo()?

use input.nextLine(); after your nextInt() function for example:- input.nextInt(); ...READ MORE

answered May 11, 2019 in Java by Sourav Dangi
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: ...READ MORE

answered Dec 20, 2019 in Java by Roberto
• 460 points

edited Jul 6, 2020 by Roberto 14,585 views
0 votes
1 answer

Gracefully stopping a java thread

The preferable way will be to use a ...READ MORE

answered Aug 20, 2018 in Java by anto.trigg4
• 3,440 points