我想了解如何将日期转换为俄罗斯日期格式。我使用 SimpleDateFormat, Date 和 DateFormat 类来实现这一点。我的代码示例如下:
static void convertToRusDate(String dateStr) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
Date date = simpleDateFormat.parse(dateStr);
DateFormat russianLanguageDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("ru"));
System.out.println("Date in the Russian language format: " + russianLanguageDateFormat.format(date));
}
public static void main(String[] args) throws ParseException {
convertToRusDate("2012-01-31 23:59:59.999+0100");
}
我看到的结果类似于下面的输出:
Date in the Russian language format: 01.02.2012
为什么输出看起来像 01.02.2012 而不是 31.01.2012?
你能帮忙吗?
谢谢, 阿列克谢
答案 0 :(得分:2)
您的 russianLanguageDateFormat
将在指定的 Locale
中显示日期。它不会更改显示日期的时区,默认情况下将是您自己的系统时区。我的系统字体上没有俄语字符,所以我将其更改为 new Locale("en-uk")
。我还使用了 getDateTimeInstance
而不是 getDateInstance
,所以很清楚发生了什么。
DateFormat englishLanguageDateFormat =
DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL,
// new Locale("ru"));
new Locale("en-uk"));
System.out.println("Date in the English language format: " +
englishLanguageDateFormat.format(date));
..会给我:
Date in the English language format: 2012 Jan 31, Tue 23:59:59 Central European Time
...因为我(我的系统是)在 GMT+1,通过获取时区可以看出:
System.out.println("englishLanguageDateFormat.getTimeZone(): " +
englishLanguageDateFormat.getTimeZone());
..它打印并对应于我的操作系统设置:
englishLanguageDateFormat.getTimeZone(): sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
您可以使用
更改格式化字符串的时区 englishLanguageDateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("englishLanguageDateFormat.getTimeZone(): " +
englishLanguageDateFormat.getTimeZone());
System.out.println("Date in the English language format: " +
englishLanguageDateFormat.format(date));
...将打印:
englishLanguageDateFormat.getTimeZone(): sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Date in the English language format: 2012 Jan 31, Tue 14:59:59 Pacific Standard Time