tags:

views:

535

answers:

5

I have my app hosted in a London Server. I am in Madrid, Spain. So the timezone is -2 hours.

How can I obtain the current date / time with my time zone.

Date curr_date = new Date(System.currentTimeMillis());

e.g.

Date curr_date = new Date(System.currentTimeMillis("MAD_TIMEZONE"));

With JODA

DateTimeZone zone = DateTimeZone.forID("Europe/Madrid");
DateTime dt = new DateTime(zone);
int day = dt.getDayOfMonth();
int year = dt.getYear();
int month = dt.getMonthOfYear();
int hours = dt.getHourOfDay();
int minutes = dt.getMinuteOfHour();
+7  A: 

Date is always UTC-based. There's no notion of a "local instance of Date." Use Date in conjunction with Calendar and/or TimeZone.getDefault() to use a "local" time zone. Use TimeZone.getTimeZone("Europe/Madrid") to get the Madrid time zone.

... or use Joda Time, which tends to make the whole thing clearer, IMO.

Jon Skeet
@Downvoter: Care to say why?
Jon Skeet
+1  A: 

You would use JodaTime for that. Java.util.Date is very limited regarding TimeZone.

jpartogi
java.util.Date is deliberately divorced from TimeZone. For simple "what is the time in a particular time zone" java.util.* isn't too bad... but I agree that Joda Time is simply a better API in general.
Jon Skeet
+3  A: 

using Calendar is simple:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/Madrid"));
Date currentDate = calendar.getTime();
dfa
Won't "GMT-2" give the "always GMT-2" zone, which won't include daylight savings?
Jon Skeet
you're right, I'm looking for a timezone sheet for handling daylight savings (I was not sure about -2)
dfa
@downvoter: please explain your downvote or it is pointless
dfa
Because stackoverflow is not Google. So answering trivial questions provoke people asking more stupid questions instead of reading documentation and googling.
stepancheg
please read the stackoverflow FAQ at http://stackoverflow.com/faq:"No question is too trivial or too "newbie". Oh yes, and it should be about programming."
dfa
FAQ is wrong. There must be an item: "Spend 5 minutes googling and reading documentation before asking". Of course newbie questions that are not answerable by Google are OK.
stepancheg
The FAQ is correct. If someone Googles **any** programming question, we'd like Stack Overflow to be the top search result (or very near to it). This means that even the most trivial question, if it does not already exist on SO, is fair game.
Bill the Lizard
A: 

Here is an example:

Calendar c = Calendar.getInstance(TimeZone.getDefault());
Date date = c.getTime();
Martijn Courteaux
+2  A: 

As Jon Skeet already said, java.util.Date does not have a time zone. A Date object represents a number of milliseconds since January 1, 1970, 12:00 AM, UTC. It does not contain time zone information.

When you format a Date object into a string, for example by using SimpleDateFormat, then you can set the time zone on the DateFormat object to let it know in which time zone you want to display the date and time:

Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// Use Madrid's time zone to format the date in
df.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));

System.out.println("Date and time in Madrid: " + df.format(date));

If you want the local time zone of the computer that your program is running on, use:

df.setTimeZone(TimeZone.getDefault());
Jesper