Java中用于日期格式的代码如下所示:’Jun 3, 2020 5:04:05 PM’。

huangapple go评论81阅读模式
英文:

Datetime format in java for date like 'Jun 3, 2020 5:04:05 PM'

问题

在Java中是否有一种格式用于解析日期,例如像'Jun 3, 2020 5:04:05 PM'这样的格式?因为我正在使用JEE创建一个应用程序,在持久化数据时,我的日志文件中出现了以下消息:<Text 'Jun 3, 2020 5:04:05 PM' 无法在索引0处解析>。

英文:

Is there an format in java for parsing date such as 'Jun 3, 2020 5:04:05 PM' because i'm creating an app in JEE and i got ths message <Text 'Jun 3, 2020 5:04:05 PM' could not be parsed at index 0> in my log file when persisting data

答案1

得分: 2

I recommend you use java.time.LocalDateTime and DateTimeFormatter.ofPattern instead of using the outdated java.util.Date and SimpleDateFormat. Check this to learn more about the modern date/time API.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        // Define format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a", Locale.US);

        // Date/time string
        String strDate = "Jun 3, 2020 5:04:05 PM";

        // Parse the date/time string into LocalDateTime
        LocalDateTime ldt = LocalDateTime.parse(strDate, formatter);

        // Display ldt.toString()
        System.out.println(ldt);

        // Display `ldt` in the specified format
        System.out.println(formatter.format(ldt));
    }
}

Output:

2020-06-03T17:04:05
Jun 3, 2020 5:04:05 PM
英文:

I recommend you use java.time.LocalDateTime and DateTimeFormatter.ofPattern instead of using the outdated java.util.Date and SimpleDateFormat. Check this to learn more about the modern date/time API.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

	public static void main(String[] args) {
		// Define format
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;MMM d, yyyy h:mm:ss a&quot;, Locale.US);

		// Date/time string
		String strDate = &quot;Jun 3, 2020 5:04:05 PM&quot;;

		// Parse the date/time string into LocalDateTime
		LocalDateTime ldt = LocalDateTime.parse(strDate, formatter);

		// Display ldt.toString()
		System.out.println(ldt);

		// Display `ldt` in the specified format
		System.out.println(formatter.format(ldt));
	}
}

Output:

2020-06-03T17:04:05
Jun 3, 2020 5:04:05 PM

答案2

得分: 1

如果我理解你的意思正确的话:

String dString = "2020年6月3日 下午5:04:05";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年M月d日 a h:mm:ss", Locale.CHINA);
Date date = formatter.parse(dString);

编辑:
处理地区信息:

new SimpleDateFormat("yyyy年M月d日 a h:mm:ss", Locale.US);

你也可以查看 LocalDateTimeDateTimeFormatter

英文:

If I understand you correctly:

String dString =  &quot;Jun 3, 2020 5:04:05 PM&quot; 
SimpleDateFormat formatter = new SimpleDateFormat(&quot;MMM dd, yyyy HH:mm:ss aa&quot;);
Date date = formater.parse(dString);

EDIT:
Dealing with locale:

new SimpleDateFormat(&quot;MMM dd, yyyy HH:mm:ss aa&quot;, Locale.US)

You can also look at LocalDateTime and DateTimeFormatter

huangapple
  • 本文由 发表于 2020年6月5日 21:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定