如何在 Java 中获取时区的当前日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1305350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current date and time of your timezone in Java?
提问by Sergio del Amo
I have my app hosted in a London Server. I am in Madrid, Spain. So the timezone is -2 hours.
我的应用程序托管在伦敦服务器中。我在西班牙马德里。所以时区是-2小时。
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();
采纳答案by Jon Skeet
Date
is alwaysUTC-based... or time-zone neutral, depending on how you want to view it. A Date
onlyrepresents 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 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.
Date
是始终基于UTC ...或时区中立的,这取决于你想如何看待它。甲Date
只表示在一个时间点; 它与时区无关,自 Unix 纪元以来只有几毫秒。没有“本地实例”的概念Date
。使用Date
会同Calendar
和/或TimeZone.getDefault()
使用“本地”时区。使用TimeZone.getTimeZone("Europe/Madrid")
得到马德里时区。
... 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.
...或使用Joda Time,这往往会使整个事情更清晰,IMO。在 Joda Time 中,您将使用一个DateTime
值,它是特定日历系统和时区中的一个瞬间。
In Java 8 you'd use java.time.ZonedDateTime
, which is the Java 8 equivalent of Joda Time's DateTime
.
在 Java 8 中,您会使用java.time.ZonedDateTime
,它是 Java 8 中 Joda Time 的DateTime
.
回答by Joshua Partogi
回答by dfa
回答by Martijn Courteaux
Here is an example:
下面是一个例子:
Calendar c = Calendar.getInstance(TimeZone.getDefault());
Date date = c.getTime();
回答by Jesper
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.
正如 Jon Skeet 已经说过的,java.util.Date
没有时区。一个Date
对象表示自1970年1月1日,上午12:00,UTC毫秒数。它不包含时区信息。
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 对象格式化为字符串时,例如使用SimpleDateFormat
,您可以在DateFormat
对象上设置时区,让它知道您希望在哪个时区显示日期和时间:
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());
回答by wjin
I couldn't get it to work using Calendar. You have to use DateFormat
我无法使用日历使其正常工作。你必须使用 DateFormat
//Wednesday, July 20, 2011 3:54:44 PM PDT
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df.setTimeZone(TimeZone.getTimeZone("PST"));
final String dateTimeString = df.format(new Date());
//Wednesday, July 20, 2011
df = DateFormat.getDateInstance(DateFormat.FULL);
df.setTimeZone(TimeZone.getTimeZone("PST"));
final String dateString = df.format(new Date());
//3:54:44 PM PDT
df = DateFormat.getTimeInstance(DateFormat.FULL);
df.setTimeZone(Timezone.getTimeZone("PST"));
final String timeString = df.format(new Date());
回答by shamim
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df.setTimeZone(TimeZone.getTimeZone("PST"));
final String dateTimeString = df.format(new Date());
回答by Subhamay
Check this may be helpful. Works fine for me. Code also covered daylight savings
检查这可能会有所帮助。对我来说很好用。代码还涵盖了夏令时
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
Calendar cal = Calendar.getInstance();
// If needed in hours rather than milliseconds
int LocalOffSethrs = (int) ((cal.getTimeZone().getRawOffset()) *(2.77777778 /10000000));
int ChinaOffSethrs = (int) ((tz.getRawOffset()) *(2.77777778 /10000000));
int dts = cal.getTimeZone().getDSTSavings();
System.out.println("Local Time Zone : " + cal.getTimeZone().getDisplayName());
System.out.println("Local Day Light Time Saving : " + dts);
System.out.println("China Time : " + tz.getRawOffset());
System.out.println("Local Offset Time from GMT: " + LocalOffSethrs);
System.out.println("China Offset Time from GMT: " + ChinaOffSethrs);
// Adjust to GMT
cal.add(Calendar.MILLISECOND,-(cal.getTimeZone().getRawOffset()));
// Adjust to Daylight Savings
cal.add(Calendar.MILLISECOND, - cal.getTimeZone().getDSTSavings());
// Adjust to Offset
cal.add(Calendar.MILLISECOND, tz.getRawOffset());
Date dt = new Date(cal.getTimeInMillis());
System.out.println("After adjusting offset Acctual China Time :" + dt);
回答by shan
Here are some steps for finding Time for your zone:
以下是为您的区域查找时间的一些步骤:
Date now = new Date();
现在日期 = 新日期();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println("timeZone.......-->>>>>>"+df.format(now));
回答by Zeeshan
With the java.timeclasses built into Java 8 and later:
使用Java 8 及更高版本中内置的java.time类:
public static void main(String[] args) {
LocalDateTime localNow = LocalDateTime.now(TimeZone.getTimeZone("Europe/Madrid").toZoneId());
System.out.println(localNow);
// Prints current time of given zone without zone information : 2016-04-28T15:41:17.611
ZonedDateTime zoneNow = ZonedDateTime.now(TimeZone.getTimeZone("Europe/Madrid").toZoneId());
System.out.println(zoneNow);
// Prints current time of given zone with zone information : 2016-04-28T15:41:17.627+02:00[Europe/Madrid]
}