SimpleDateFormat 在 java 中返回错误的日期

发布于 2025-01-17 23:47:39 字数 670 浏览 2 评论 0原文

private String dateFormatter(String olddate) {
   String newDate = "";
   try {
       SimpleDateFormat initDate = new SimpleDateFormat("dd-MMM-yy - HH:mm");
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy - HH:mm");
       newDate = formatter.format(initDate.parse(olddate));
   } catch (ParseException e) {
       e.printStackTrace();
   }
   return newDate;
}

输入日期为:29-MAR-22-22:00 所需的输出日期为:29/03/22-22:00

而不是此,我将获得解析异常 当我将当前日期转换为 dd-mmm-yy-hh:mm 格式,它返回29-m03-22-11:38是错误的。

因此,请帮助我解决这个问题。

提前致谢。

private String dateFormatter(String olddate) {
   String newDate = "";
   try {
       SimpleDateFormat initDate = new SimpleDateFormat("dd-MMM-yy - HH:mm");
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy - HH:mm");
       newDate = formatter.format(initDate.parse(olddate));
   } catch (ParseException e) {
       e.printStackTrace();
   }
   return newDate;
}

Input Date is : 29-Mar-22 - 22:00
Required Output Date is : 29/03/22 - 22:00

Instead of this I will get parse exception
When I convert current date in dd-MMM-yy - HH:mm format it returns 29-M03-22 - 11:38 Which is wrong.

So please help me to solve this issue.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-01-24 23:47:39

确保您的格式模式使用字母m US-ASCII范围Unicode的范围,如 dawood ibn kareem 评论。

java.time

您正在使用几年前被现代 java.Time 类取代的可怕日期时间类

LocalDateTime
.parse(
    "29-Mar-22 - 22:00" ,
    DateTimeFormatter.ofPattern( "dd-MMM-uu - HH:mm" ).withLocale( Locale.US ) 
)
.format(
    DateTimeFormatter.ofPattern( "dd/MM/uu - HH:mm" )
)

。 ://ideone.com/ym2alp“ rel =“ nofollow noreferrer”>代码在ideone.com 中实时运行。

29/03/22-22:00


提示:使用四位数。以我的经验,保存两个数字的空间不值得歧义引起的混乱。

提示:(a)仅使用标准 ISO 8601 数据交换,(b)令 java.time 在生成字符串值以呈现给用户时自动本地化。

LocalDateTime
        .parse(
            "29-Mar-22 - 22:00" ,
            DateTimeFormatter.ofPattern( "dd-MMM-uu - HH:mm" ).withLocale( Locale.US ) 
        )
        .format(
            DateTimeFormatter
            .ofLocalizedDateTime( FormatStyle.SHORT )
            .withLocale( new Locale( "es" , "AR ") )  // Spanish language, Argentina culture.
        )

29/3/22 22:00

Be sure your formatting pattern is using the letter M from the US-ASCII range of Unicode, as commented by Dawood ibn Kareem.

java.time

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

LocalDateTime
.parse(
    "29-Mar-22 - 22:00" ,
    DateTimeFormatter.ofPattern( "dd-MMM-uu - HH:mm" ).withLocale( Locale.US ) 
)
.format(
    DateTimeFormatter.ofPattern( "dd/MM/uu - HH:mm" )
)

See this code run live at IdeOne.com.

29/03/22 - 22:00


Tip: Use four digit years. In my experience, saving two digits of space is not worth the confusion caused by the ambiguity.

Tip: Rather than hard-code such formats, (a) use only standard ISO 8601 formats for data exchange, and (b) let java.time automatically localize when producing string values for presentation to the user.

LocalDateTime
        .parse(
            "29-Mar-22 - 22:00" ,
            DateTimeFormatter.ofPattern( "dd-MMM-uu - HH:mm" ).withLocale( Locale.US ) 
        )
        .format(
            DateTimeFormatter
            .ofLocalizedDateTime( FormatStyle.SHORT )
            .withLocale( new Locale( "es" , "AR ") )  // Spanish language, Argentina culture.
        )

29/3/22 22:00

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文