Parse function unable to parse string and throwing error in Java

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

Parse function unable to parse string and throwing error in Java

问题

无法解析以下日期。出现解析异常。请帮助查找错误:

String myDate = "2020–03–01 3:15 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.ENGLISH);
Date date = sdf.parse(myDate);
英文:

Not able to parse the following date. Getting parse exception. Please help in finding error :

            String myDate = "2020–03–01 3:15 pm";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm aa",Locale.ENGLISH);
            Date date = sdf.parse(myDate);

答案1

得分: 2

根据外观,您的日期字符串包含 (短划线)而不是 -(连字符)。

尝试在日期中使用连字符,看看是否能够正确解析。

奖励ASCII表详细信息:

短划线 (-):

Parse function unable to parse string and throwing error in Java

.

连字符 (-):

Parse function unable to parse string and throwing error in Java

英文:

From the looks of it your date string contains (dash) instead of - (hyphen).

Try using hyphens in the date instead and see if it manages to parse it correctly.

Bonus ascii table details:

Dash (-):

Parse function unable to parse string and throwing error in Java

.

Hyphen (-):

Parse function unable to parse string and throwing error in Java

答案2

得分: 2

你所使用的分隔符来分隔年份、月份和日期似乎不正确。我建议您重新键入日期时间字符串,而不是从其他地方复制粘贴。我还建议您从过时的日期时间 API 切换到现代的 API。

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

public class Main {
    public static void main(String[] args) {
        String myDate = "2020-03-01 3:15 pm";
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                            .parseCaseInsensitive()
                                            .appendPattern("u-M-d h:m a")
                                            .toFormatter(Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(myDate, formatter);
        System.out.println(ldt);
    }
}

输出结果:

2020-03-01T15:15

如果您仍然想要使用传统的日期时间 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 myDate = "2020-03-01 3:15 pm";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm aa", Locale.ENGLISH);
        Date date = sdf.parse(myDate);
        System.out.println(date);
    }
}

输出结果:

Sun Mar 01 15:15:00 GMT 2020

请注意,我使用了单个 h 来匹配您的日期时间字符串。

英文:

The separator character that you have used to separate the year, the month and the day doesn't seem to be correct. I suggest you type the date-time string again instead of copying and pasting it from somewhere. I also recommend you switch from the outdated date-time API to the modern one.

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

public class Main {
	public static void main(String[] args) {
		String myDate = "2020-03-01 3:15 pm";
		DateTimeFormatter formatter = new DateTimeFormatterBuilder()
											.parseCaseInsensitive()
											.appendPattern("u-M-d h:m a")
											.toFormatter(Locale.ENGLISH);
		LocalDateTime ldt = LocalDateTime.parse(myDate, formatter);
		System.out.println(ldt);
	}
}

Output:

2020-03-01T15:15

If you still want to use the legacy date-time API, you can do it as follows:

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 myDate = "2020-03-01 3:15 pm";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm aa", Locale.ENGLISH);
		Date date = sdf.parse(myDate);
		System.out.println(date);
	}
}

Output:

Sun Mar 01 15:15:00 GMT 2020

Note that I've used a single h to match your date-time string.

huangapple
  • 本文由 发表于 2020年8月1日 16:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63203062.html
匿名

发表评论

匿名网友

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

确定