用java获取系统时间

用java获取系统时间,java,jodatime,Java,Jodatime,我使用上面的代码获取系统时间,并将其安装在WebSphereServer中 实际上,它只返回服务器启动时间,而不是当前时间。请指导我获取当前时间。如果您想要当前日期和时间,可以使用以下选项之一: static DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy HH:mma"); static DateTimeZone zone = DateTimeZone.forID("America/New_York"); static Chr

我使用上面的代码获取系统时间,并将其安装在WebSphereServer中


实际上,它只返回服务器启动时间,而不是当前时间。请指导我获取当前时间。

如果您想要当前日期和时间,可以使用以下选项之一:

static DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy HH:mma");
static DateTimeZone zone = DateTimeZone.forID("America/New_York");
static Chronology coptic = GJChronology.getInstance(zone);
static DateTime dt = new DateTime(coptic);
System.out.println("time is :::"+ dateFormat.format(dt.toDate()));
或者使用Calendar.getInstance

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date));
Javadoc说:

在可能的情况下,建议改用等时法

因此,您只需使用:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate = dateFormat.format(cal.getTime());
要打印日期/时间,您应该将HH小写或删除a,因为带有AM/PM的24小时时钟是错误的:

DateTime dt = DateTime.now();

此程序返回程序运行的时间。假设服务器在晚上7点启动,应用程序在晚上10点触发,那么它不会只返回晚上7点而不是10点。请告诉我在哪里我需要更改它似乎你想要科普特人的表现,但选择一个格式化程序SimpleDateFormat,它不能显示科普特人的日历日期。但是如果你选择Joda Time的DateTimeFormatter,那么你肯定不会得到科普特的月份名称,而只会得到数字。这是你真正的问题吗?顺便说一句:年代不是科普特人,而是朱利安/格里高利。
DateTimeFormatter dateFormat = DateTimeFormat.forPattern("MMM dd yyyy hh:mm a")
                               .withZone(DateTimeZone.forID("America/New_York"));
System.out.println("time is :::"+ dt.toString(dateFormat));