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();
using Calendar is simple:
I couldn't get it to work using Calendar. You have to use DateFormat
As Jon Skeet already said,
java.util.Date
does not have a time zone. ADate
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 theDateFormat
object to let it know in which time zone you want to display the date and time:If you want the local time zone of the computer that your program is running on, use:
With the java.time classes built into Java 8 and later:
Here is an example: