gpt4 book ai didi

java - 获取正确的月份格式日期java

转载 作者:行者123 更新时间:2023-12-01 18:37:48 24 4
gpt4 key购买 nike

有什么帮助吗,如何从“2014-01-10T09:41:16.000+0000”这样的字符串中获取正确的日期我的代码是:

    String strDate = "2014-01-10T09:41:16.000+0000";
String day = "";
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
Locale locale = new Locale("es", "ES");
SimpleDateFormat formater = new SimpleDateFormat(format, locale);
formater.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));

Calendar cal = Calendar.getInstance();

try {
cal.setTimeInMillis(formater.parse(strDate).getTime());
String offerDate = cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR);
System.out.println(offerDate);
} catch (Exception e){
System.out.println(e.getMessage());
}

在结果中我给出这样的内容:“10-0-2014”,我想要像“10-01-2014”这样的结果

提前致谢:)

最佳答案

文档指出:

java.util.Calendar.MONTH

MONTH public static final int MONTH Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

-> Calendar.MONTH 从 0 开始计数

关于java - 获取正确的月份格式日期java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21137941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com