无法在Java中以波兰语语言环境解析格式为d MMMM yyyy的日期 [英] Unable to Parse a date of format d MMMM yyyy in Polish Locale in Java

查看:73
本文介绍了无法在Java中以波兰语语言环境解析格式为d MMMM yyyy的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用波兰语语言环境解析格式为d MMMM yyyy的日期(2020年12月3日),但无法解析.但是,为什么在任何其他语言环境(例如英语)中相同的解析都可以正常工作.下面是无法正常工作的代码示例.有人可以帮忙吗?

I tried to parse the date (3 December, 2020) with format d MMMM yyyy in Polish Locale but it is unable to parse. But why the same parsing is working fine in any other locale like english, etc. Below is the code sample which is not working. Can anyone please help on this ?

    Locale loc = new Locale("pl", "PL");
    String date = "3 December 2020";
    SimpleDateFormat sdFormat =
            new SimpleDateFormat("d MMMM yyyy", loc);
    sdFormat.setLenient(false);
    try {
        Date d = sdFormat.parse(date);
        System.out.println(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

推荐答案

似乎您对解析格式感到困惑.

由于您输入的日期字符串为 English ,因此您需要使用 Locale.ENGLISH 进行解析,并且需要另一个实例SimpleDateFormat Locale("pl","PL") 格式化,将获得的 java.util.Date 对象格式化为 new Locale("pl","PL").

Since your input date string in English, you need to use Locale.ENGLISH for parsing and you need another instance of SimpleDateFormat with Locale("pl", "PL") to format the obtained java.util.Date object with new Locale("pl", "PL").

演示:

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[]) {
        Locale loc = new Locale("pl", "PL");
        String date = "3 December 2020";
        SimpleDateFormat sdfForParsing = new SimpleDateFormat("d MMMM yyyy", Locale.ENGLISH);
        SimpleDateFormat sdfForFormatting = new SimpleDateFormat("d MMMM yyyy", loc);
        sdfForParsing.setLenient(false);
        try {
            Date d = sdfForParsing.parse(date);
            System.out.println(d);
            String localiseByPolish = sdfForFormatting.format(d);
            System.out.println(localiseByPolish);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

输出:

Thu Dec 03 00:00:00 GMT 2020
3 grudnia 2020

我相信您已经知道日期时间对象仅存储日期时间信息 * 1 没有格式信息.在打印时,日期时间对象将打印其类的 toString 实现返回的字符串.另外, java.util.Date 对象并不代表真正的日期时间类,因为它仅存储毫秒(例如, new Date()>对象会以从 1970年1月1日格林尼治标准时间00:00:00(格林尼治标准时间)开始的毫秒数进行实例化),然后在您打印时,它会计算JVM时区中的日期时间并打印相同的日期,即如果您在世界任何地方的指定时间执行以下两行,

I believe you already know that a date-time object stores just the date-time information*1and no formatting information. On printing, a date-time object prints the string returned by the toString implementation of its class. Also, a java.util.Date object does not represent a true date-time class as it stores just the milliseconds (e.g. new Date() object is instantiated with the number of milliseconds from January 1, 1970, 00:00:00 GMT) and when you print it, it calculates the date-time in your JVM's timezone and prints the same i.e. if your execute the following two lines at a given moment in any part of the world,

Date date = new Date();
System.out.println(date.getTime());

您将获得相同的号码.查看此答案以获取演示.

you will get the same number. Check this answer for a demo.

java.util 的日期时间API及其格式API SimpleDateFormat 已经过时,并且由于存在如此多的此类hack,因此容易出错.建议完全停止使用它们,并切换到现代日期时间API .在 跟踪:日期时间 .

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and because of so many such hacks, they are error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

注意:如果您正在为Android项目工作,而您的Android API级别仍不符合Java-8,请检查如何在Android Project中使用ThreeTenABP .

Note: If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

使用现代的日期时间API:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        Locale loc = new Locale("pl", "PL");
        String date = "3 December 2020";
        DateTimeFormatter dtfForParsing = DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.ENGLISH)
                                            .withResolverStyle(ResolverStyle.LENIENT);
        DateTimeFormatter dtfForFormatting = DateTimeFormatter.ofPattern("d MMMM yyyy", loc);
        LocalDate localeDate = LocalDate.parse(date, dtfForParsing);
        System.out.println(localeDate);
        String localiseByPolish = localeDate.format(dtfForFormatting);
        System.out.println(localiseByPolish);
    }
}

输出:

2020-12-03
3 grudnia 2020


* 1 现代日期时间API 还存储时区信息.检查概述,以了解有关这些类的更多信息.


*1The modern date-time API store also the timezone information. Check Overview to learn more about these classes.

这篇关于无法在Java中以波兰语语言环境解析格式为d MMMM yyyy的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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