date= Calendar.getInstance();
Date currentDate = date.getTime();
String sDate = currentDate.toString();

This returns time EST. I need to change it to Arizona time which is tricky because Arizona does not have daylight savings time. Is there a short cut to making the changes or do I need to query a calendar to subtract two hours when Arizona is on MST and three hours when PST.

有帮助吗?

解决方案

This returns time EST.

Well, Date.toString() will, if you're in EST at the moment. It's not part of the data stored within the Date - that's just an instant in time, with no idea what time zone or calendar system it might have started off in.

Your first two lines would be more simply written as:

Date currentDate = new Date();

You should use a DateFormat to convert the Date into a String. You can specify the time zone you want to use there. Do not start performing any arithmetic on the date yourself to add/remove offsets - that's a sign that you're heading in the wrong direction.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top