使用SimpleDateFormat在Java中解析日期

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

Date parsing in Java using SimpleDateFormat

问题

我想解析这种格式的日期: "Wed Aug 26 2020 11:26:46 GMT+0200" 成为一个日期。但我不知道该如何做。我尝试了这个代码:

SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(split[0]); //错误行
String formattedDate = formatter.format(date);

我得到了这个错误:无法解析的日期: "Wed Aug 26 2020 11:26:46 GMT+0200"。我的日期格式是否错误?如果是的话,有人能指导我正确的方向吗?

英文:

I want to parse a date in this format: "Wed Aug 26 2020 11:26:46 GMT+0200" into a date. But I don't know how to do it. I tried this:

SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(split[0]); //error line
String formattedDate = formatter.format(date);

I am getting this error: Unparseable date: "Wed Aug 26 2020 11:26:46 GMT+0200". Is my date format wrong? And if so could somebody please point me in the right direction?

答案1

得分: 2

我建议你停止使用过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat。转而使用现代的 java.time 日期时间 API 以及相应的格式化 API (java.time.format)。从**教程:日期时间**中了解更多关于现代日期时间 API 的信息。

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

public class Main {
    public static void main(String[] args) {
        // 给定的日期时间字符串
        String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

        // 将给定的日期时间字符串解析为 OffsetDateTime
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr,
                DateTimeFormatter.ofPattern("E MMM d u H:m:s zX", Locale.ENGLISH));

        // 显示 OffsetDateTime
        System.out.println(odt);
    }
}

输出:

2020-08-26T11:26:46+02:00

使用遗留的 API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        // 给定的日期时间字符串
        String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

        // 定义格式化器
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);

        // 将给定的日期时间字符串解析为 java.util.Date
        Date date = parser.parse(dateTimeStr);
        System.out.println(date);
    }
}

输出:

Wed Aug 26 10:26:46 BST 2020
英文:

I suggest you stop using the outdated and error-prone java.util date-time API and SimpleDateFormat. Switch to the modern java.time date-time API and the corresponding formatting API (java.time.format). Learn more about the modern date-time API from Trail: Date Time.

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

public class Main {
	public static void main(String[] args) {
		// Given date-time string
		String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

		// Parse the given date-time string to OffsetDateTime
		OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr,
				DateTimeFormatter.ofPattern("E MMM d u H:m:s zX", Locale.ENGLISH));

		// Display OffsetDateTime
		System.out.println(odt);
	}
}

Output:

2020-08-26T11:26:46+02:00

Using the legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
	public static void main(String[] args) throws ParseException {
		// Given date-time string
		String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

		// Define the formatter
		SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);

		// Parse the given date-time string to java.util.Date
		Date date = parser.parse(dateTimeStr);
		System.out.println(date);
	}
}

Output:

Wed Aug 26 10:26:46 BST 2020

huangapple
  • 本文由 发表于 2020年8月26日 17:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63594799.html
匿名

发表评论

匿名网友

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

确定