wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to get java Date with Daylight savings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get java Date with Daylight savings" Watch "How to get java Date with Daylight savings" New topic
Author

How to get java Date with Daylight savings

Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Hello,

If i apply daylight savings and e.g selects PST then java new Date() returns wrong time and date.

So how can i fix this issue and get date with applying daylight savings.

E.g. System.out.println( new Date()); gives me 03:19 AM but current time is 04:19.


Your help will be really appreciated.


Thank You.
Have a Nice Time.

-Santosh


shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
You should be using java calender API, where you can set time zone you want with day light saving. When you print Date it takes default time zone of JVM and print date accordingly.


SCJP 1.5(97%) My Blog
Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Can you please give me block of code to print current time with daylight savings
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
You might find this link useful.

http://www.javakb.com/Uwe/Forum.aspx/java-programmer/43489/Daylight-Savings-Time-in-Calendar
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Try this code:



-Varun -
(My Blog) - Online Certifications - Webner Solutions
Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Hello,

Thanks for your efforts.

Here is solution,

GregorianCalendar gc = new GregorianCalendar();
TimeZone tz = gc.getTimeZone();
gc.add(GregorianCalendar.MILLISECOND, +tz.getDSTSavings());
System.out.println(gc.getTime());


Thank You.
Have a Nice Time.

-Santosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get java Date with Daylight savings