java - 为什么日期输出看起来像这样?
问题描述
我试图了解如何将日期转换为俄罗斯日期格式。我使用 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?
能否请你帮忙?
谢谢, 阿列克谢
解决方案
您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());
..which 打印并对应于我的操作系统设置为:
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
推荐阅读
- javascript - Cannot access function through this
- sql - Auto-generating value for a field based on the value of primary key, current year and previous year
- python - 将 shared_memory 与 Pandas Dataframe 一起使用
- python - 通过python从文件中获取不同的值
- regex - 多个cookie的Apache mod_headers正则表达式?
- google-apps-script - 清除单元格后用于擦除时间戳的 Google 表格脚本
- mysql - 无法连接到 ODBC DSN
- sonarqube - 如何从默认更改 sonarqube 工作路径
- javascript - 使用对象中定义的属性更改 CSS
- javascript - 如何使用nodejs在elasticsearch中的聚合中使用自定义脚本