“EEE MMM dd HH:mm:ss ZZZ yyyy"日期格式为 java.sql.Date [英] "EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date

查看:21
本文介绍了“EEE MMM dd HH:mm:ss ZZZ yyyy"日期格式为 java.sql.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 EEE MMM dd HH:mm:ss ZZZ yyyy 转换为 YYYY-MM-DD 格式,以便我可以将其插入到 MySQL 数据库中.我没有收到任何错误,但插入到我的数据库中的日期是错误的,并且每一行都相同......

I am trying to convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-DD format, so I can insert it into a MySQL database. I do not get any error, but the date inserted into my db is wrong and the same for every row...

 String date = Sat Mar 04 09:54:20 EET 2017;
 SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy"); 
 SimpleDateFormat formatneeded=new SimpleDateFormat("YYYY-MM-DD");

 java.util.Date date1 = (java.util.Date)formatnow.parse(date);
 String date2 = formatneeded.format(date1);
 java.util.Date date3= (java.util.Date)formatneeded.parse(date2);


 java.sql.Date sqlDate = new java.sql.Date( date3.getTime() );
 pst.setDate(1, sqlDate);

推荐答案

    LocalDate date4 = ZonedDateTime
            .parse(date, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH))
            .toLocalDate();
    java.sql.Date date5 = java.sql.Date.valueOf(date4);

我正在使用 java.time 包中的现代类.你会发现代码不仅更简单,熟悉了新类流畅的书写风格,也更清晰.

I am using the modern classes in the java.time package. You notice that the code is not only simpler, once you get acquainted with the fluent writing style of the newer classes, it is also clearer.

如果您想 100% 现代,您还应该检查您最新的 MySQL JDBC 驱动程序是否不直接接受 LocalDate 而无需转换为 java.sql.Date.应该.

If you wanted to be 100 % modern, you should also check out whether your latest MySQL JDBC driver wouldn’t accept a LocalDate directly without conversion to java.sql.Date. It should.

一些需要注意的细节

  • 如果您需要您的代码在您无法控制的计算机上运行,​​请始终为格式化程序提供语言环境,否则无法在非英语语言环境的计算机上解析您的日期字符串.您可以将 Locale.ROOT 用于语言环境中性语言环境(它说英语).
  • 如果可以,请避免使用三个字母的时区缩写.很多都是模棱两可的.EET 实际上只有半个时区,因为现在一些使用它的地方是 EEST(夏令时).最好使用像 Europe/Bucharest 这样的长时区 ID 或像 +02:00 这样的 UTC 偏移量.
  • If you need your code to run on computers outside your control, always give locale to your formatter, or your date string cannot be parsed on a computer with a non-English-speaking locale. You may use Locale.ROOT for a locale neutral locale (it speaks English).
  • If you can, avoid the three letter time zone abbreviations. Many are ambiguous. EET is really only half a time zone since some places where it’s used are on EEST (summer time) now. Better to use either a long time zone ID like Europe/Bucharest or an offset from UTC like +02:00.

无论您使用DateTimeFormatter 还是SimpleDateFormat,这些点都是有效的.

These points are valid no matter if you use DateTimeFormatter or SimpleDateFormat.

如果您不能或不想继续使用推荐的较新类,对您的代码的修复是:

If you cannot or do not want to move on to the recommended newer classes, the fix to your code is:

    SimpleDateFormat formatnow 
            = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); 
    SimpleDateFormat formatneeded = new SimpleDateFormat("yyyy-MM-dd");

我使用的是小写的 zzz,因为这被记录为匹配三个字母的时区名称,我知道大写的 ZZZ 也可以使用.我添加了语言环境.也许最重要的是,在所需的格式中,我已将 YYYY(基于周的年份)更改为 yyyy(日历年)和 DD(日年)到 dd(月中的第几天).所有这些字母都在文档中.

I am using lowercase zzz since this is documented to match a three-letter time zone name, I know that uppercase ZZZ works too. I have added locale. And maybe most importantly, in the needed format I have changed YYYY (week-based year) to yyyy (calendar year) and DD (day of year) to dd (day of month). All those letters are in the documentation.

这篇关于“EEE MMM dd HH:mm:ss ZZZ yyyy"日期格式为 java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆