简体   繁体   English

Century 的日期格式

[英]Date formatting with Century

I there a way to format dates which contain century in it to common format.我有一种方法可以将其中包含世纪的日期格式化为通用格式。 Format date in cyymmdd to MM/dd/yyyy.将 cyymmdd 格式的日期格式化为 MM/dd/yyyy。

Example,例子,

Convert 1140711 to o7/11/2014

If you are talking about converting a String in format:如果您正在谈论以格式转换字符串:

1140711 1140711

to

07/11/2014 07/11/2014

    DateFormat df1 = new SimpleDateFormat("yyddMM");
    DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");
    String dateString = "1140711";

    System.out.println(df2.format(df1.parse(dateString.substring(1, dateString.length()))));

As far as I know the only date-time-library which is capable of processing century fields is Joda-Time.据我所知,唯一能够处理世纪字段的日期时间库是 Joda-Time。

LocalDate date = DateTimeFormat.forPattern("CyyMMdd").parseLocalDate("1140711");
System.out.println("Century=" + date.getCenturyOfEra()); // 20
String usFormat = DateTimeFormat.forPattern("MM/dd/yyyy").print(date);
System.out.println(usFormat); // output: 07/11/2014

As you can see the input digit "1" for century is simply ignored because Joda-Time only evaluates the other fields to a valid date.正如您所看到的,世纪的输入数字“1”被简单地忽略了,因为 Joda-Time 仅将其他字段评估为有效日期。 Personally I would reject such a century input because we are in 21th of century with the century number 20, not 1. So Joda-Time has its limits here, but are you really sure that you have a century of 1 in your input, or is it just a typo?我个人会拒绝这样的世纪输入,因为我们处于 21 世纪,世纪编号为 20,而不是 1。所以 Joda-Time 在这里有其限制,但您是否真的确定您输入的世纪为 1,或者这只是一个错字吗?

I had to do the opposite thing, convert date to cyyMMdd.我不得不做相反的事情,将日期转换为 cyyMMdd。

    String centuryCharacterOfDateFormat(LocalDate valueDate) {
    int year = valueDate.getYear();
    int yearMinus1900 = year - 1900;

    if (yearMinus1900 < 0) {
        throw new PipRuntimeException("Invalid value date, because it is before 1900. " + valueDate);
    } else if (yearMinus1900 < 100) {
        return "0";
    } else {
        String strVal = String.valueOf(yearMinus1900);
        char hundredthChar = strVal.charAt(strVal.length() - 3);
        return String.valueOf(hundredthChar);
    }
}

You could use the similar logic to do the opposite conversion.您可以使用类似的逻辑进行相反的转换。 To get year, you could add 1900 and hundreds of the first character.要获得年份,您可以添加 1900 和数百个第一个字符。

In below method in example date string follow the century date format "CYYMMDD" where C for century, YY for year, MM for month and DD for day.在示例日期字符串的下面方法中,遵循世纪日期格式“CYYMMDD”,其中 C 代表世纪,YY 代表年份,MM 代表月份,DD 代表日期。 Century values aredescribe below: if Century value is 0 then it will represent 20th century, years b/w 1900 to 1999. if Century value is 1 then it will represent 21st century, years b/w 2000 to 2999.世纪值如下所述:如果世纪值为 0,则表示 20 世纪,1900 年至 1999 年黑白。如果世纪值为 1,则表示 21 世纪,2000 年至 2999 年黑白。

public LocalDate convertedDate () { String dateString = "1230124"; public LocalDate convertedDate () { String dateString = "1230124";

int century = Integer.parseInt(dateString.substring(0, 1));
int year = Integer.parseInt(dateString.substring(1, 3));
int month = Integer.parseInt(dateString.substring(3, 5));
int day = Integer.parseInt(dateString.substring(5, 7));

LocalDate ld= LocalDate.of((century + 19) * 100 + year, month, day);

System.out.printl**strong text**n("Converted Date : "+ld);

return ld;返回 ld; } }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM