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-Time
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();
Date
is always UTC-based... or time-zone neutral, depending on how you want to view it. ADate
only represents a point in time; it is independent of time zone, just a number of milliseconds since the Unix epoch. There's no notion of a "local instance ofDate
." UseDate
in conjunction withCalendar
and/orTimeZone.getDefault()
to use a "local" time zone. UseTimeZone.getTimeZone("Europe/Madrid")
to get the Madrid time zone.... or use Joda Time, which tends to make the whole thing clearer, IMO. In Joda Time you'd use a
DateTime
value, which is an instant in time in a particular calendar system and time zone.In Java 8 you'd use
java.time.ZonedDateTime
, which is the Java 8 equivalent of Joda Time'sDateTime
.You would use JodaTime for that. Java.util.Date is very limited regarding TimeZone.
Here are some steps for finding Time for your zone:
Date now = new Date();
Check this may be helpfull. Work fine for me. Code also covered daylight savings