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
18,526 views

1 answer to this question.

0 votes

You can make use of the following DateFormat.

SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
myDate.setTimeZone(TimeZone.getTimeZone("UTC"));
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

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
• 58,130 points
828 views
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 in Java by kartik
• 30,930 points
179 views
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
• 58,130 points
327 views
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,580 points
308 views
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,640 points
198 views
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
775 views
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
5,903 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

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

edited Jul 6 by Roberto 9,766 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
195 views