File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Calendar Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Method" Watch "Calendar Method" New topic

Calendar Method

Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
import java.util.Calendar;

public class CalendarMethods
public static void main(String[] args)
Calendar c = Calendar.getInstance();
long day1 = c.getTimeInMillis();
System.out.println("new hour : "+(c.HOUR_OF_DAY));
System.out.println("add 35 days : "+c.getTime());
System.out.println("roll 35 days : "+c.getTime());
System.out.println("set to 1 : "+c.getTime());

Output :
new hour : 11
add 35 days : Tue Mar 31 11:02:43 BST 2009
roll 35 days : Wed Mar 04 11:02:43 GMT 2009
set to 1 : Sun Mar 01 11:02:43 GMT 2009

why the first getTime() method shows BST while the other two shows GMT?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
What sort of Locale are you using? It was BST here on 31st March and GMT on 1st-4th March, so that looks correct to me for my own Locale. You have had automatic correction for summer time or daylight saving time.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Daylight savings.... I would have never thought of that Campbell!

~Currently preparing for SCJP6
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I was wondering if that was the answer, but I didn't know it switched from BST to GMT. I know we had a topic a while ago where someone was adding an hour right across the DST change and couldn't figure out why there was an extra hour being added. It was just ironic that the test case was at that exact time.

When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
In Britain it is very easy to remember:
From 1970-1971: Summer time all year until the last Sunday in October 1971, when 1:59:59am was followed immediately by 1:00:00am.
1971-about 10 years ago. Summer time from 3rd Sunday in March, when 1:59:59am was followed immediately by 3:00:00am, until last Sunday in October same as above.
About last 10 years (to conform to rest of European Union): Summer time from last Sunday in March until last Sunday in October, times as before.

I was possibly being sarcastic when I said "easy."
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Brian Legg wrote:Daylight savings.... I would have never thought of that Campbell!

Well, that is what BST refers to: British Summer Time. Daylight saving time for Great Britain.
I agree. Here's the link:
subject: Calendar Method
It's not a secret anymore!