简体   繁体   English

在 Java 中将日期格式“EEE MMM dd HH:mm:ss zzzz yyyy”转换为“yyyy-MM-dd'T'HH:mm:ss”

[英]Convert date fomat “EEE MMM dd HH:mm:ss zzzz yyyy” to “yyyy-MM-dd'T'HH:mm:ss” in Java

I have the code optimisation issue.我有代码优化问题。 Currently I am converting these two formats using java LocalDateTime.目前我正在使用 java LocalDateTime 转换这两种格式。 But it takes around 80 ms - 110 ms.但它需要大约 80 毫秒 - 110 毫秒。 This is increasing my response time as well.这也增加了我的响应时间。 Is there an alternative and faster way to do this??有没有另一种更快的方法来做到这一点?

here is my code:这是我的代码:

value= Tue Jan 01 00:19:32 CET 1901

begindate = 1901-01-01T00:19:32

private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern
("EEE MMM dd HH:mm:ss zzz yyyy",Locale.ENGLISH);

LocalDateTime beginDate = LocalDateTime.parse(value, formatter);

String Isoformat = beginDate.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)


Edit : The comments by Jon Skeet and finally yourself give the answer: The first time you parse a string, the time zone database and the locale data included with the JVM need to be loaded in order to parse CET (or other time zone abbreviation in the string).编辑:Jon Skeet 的评论,最后你自己给出了答案:第一次解析字符串时,需要加载 JVM 中包含的时区数据库和语言环境数据才能解析CET (或其他时区缩写字符串)。 This takes time.这需要时间。 The next times theses data are already loaded, so parsing is fast.下次这些数据已经加载,因此解析速度很快。

You can improve the parsing time of the first string if on program startup you have time to do some time zone operation so the data are loaded.如果在程序启动时您有时间进行一些时区操作以便加载数据,您可以改进第一个字符串的解析时间。 You may try ZoneId.systemDefault() or ZoneId.systemDefault().getDisplayName(TextStyle.SHORT, Locale.ENGLISH) or simply parsing a (hardcoded) string like the ones in the database.您可以尝试ZoneId.systemDefault()ZoneId.systemDefault().getDisplayName(TextStyle.SHORT, Locale.ENGLISH)或简单地解析(硬编码)字符串,如数据库中的字符串。

Original answer : Change the database schema to store the date and time as timestamp with time zone or datetime .原始答案:更改数据库架构以将日期和时间存储为timestamp with time zonedatetime时间的时间戳。 This will save you from parsing.这将使您免于解析。 (If there's data in your table already, converting it will take time, of course, but that's only once and doesn't happen while the user is waiting for a response.) (如果您的表中已经有数据,那么转换它当然需要时间,但这只是一次,并且在用户等待响应时不会发生。)

If you can't change the database schema, here's a more direct answer to your question.如果您无法更改数据库架构,这里是您问题的更直接答案。 It's ugly code because it's written for optimization, so only use it as a last resort.这是丑陋的代码,因为它是为优化而编写的,所以只能将其用作最后的手段。

private static DateTimeFormatter monthFormatter
        = DateTimeFormatter.ofPattern("MMM", Locale.ROOT);

    int m = Month.from(monthFormatter.parse(value.subSequence(4, 7))).getValue();
    if (m < 10) {
        return value.substring(value.length() - 4) + "-0" + m + '-' + value.substring(8, 10) + 'T' + value.substring(11, 19); 
    } else {
        return value.substring(value.length() - 4) + '-' + m + '-' + value.substring(8, 10) + 'T' + value.substring(11, 19); 
    }

On my 10 years old computer this reduced the time from 1.78 milliseconds to 0.73 ms per conversion, measured with System.nanoTime() on 100 conversions.在我有 10 年历史的计算机上,这将每次转换的时间从 1.78 毫秒减少到 0.73 毫秒,使用System.nanoTime()对 100 次转换进行测量。 A 59 % reduction compared to your code.与您的代码相比减少了 59%。

You may be able to get a further reduction if you make your own HashMap lookup of the month abbreviation instead of using a DateTimeFormatter .如果您自己HashMap查找月份缩写而不是使用DateTimeFormatter ,则可能会进一步减少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将EEE MMM dd HH:mm:ss ZZZ yyyy转换为YYYY-MM-dd JAVA - Convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-dd JAVA 如何比较 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期时间? - How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java? Talend - 在“yyyy-MM-dd”中转换“EEE MMM dd hh:mm:ss z yyyy” - Talend - Transform "EEE MMM dd hh:mm:ss z yyyy" in "yyyy-MM-dd" 将时间戳EEE,d MMM yyyy HH:mm:ss GMT格式转换为yyyy-MM-dd&#39;T&#39;HH:mm:ss&#39;Z&#39;格式? - Convert a timestamp EEE, d MMM yyyy HH:mm:ss GMT format to yyyy-MM-dd'T'HH:mm:ss'Z' format? 在 Java 中将日期从 MM/dd 更改为 yyyy-MM-dd'T'HH:mm:ss.SSZ - Change date from MM/dd to yyyy-MM-dd'T'HH:mm:ss.SSZ in Java Java 格式 yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSz 到 yyyy-mm-dd HH:mm:ss - Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss 将java.sql.timestamp从yyyy-MM-dd hh:mm:ss转换为MM-dd-yyyy hh:mm:ss - Convert java.sql.timestamp from yyyy-MM-dd hh:mm:ss to MM-dd-yyyy hh:mm:ss “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 yyyy-MM-dd&#39;T&#39;HH:mm:ss 和 yyyy-MM-dd&#39;T&#39;HH:mm:ssXXX 之间的日期格式差异 - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX 如何解析 yyyy-MM-dd HH:mm:ss 到日期? - How to parse a yyyy-MM-dd HH:mm:ss to Date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM