开发者

forcing 4 digits year in java's simpledateformat

I want to validate and parse dates using a simpleDateFormat with the format "yyyymmdd" This also allows 100624, which is parsed to the year 10 (54 years after Julius Ceasar died). The dates will also be something like 1970, so I don't want to settle with SimpleDateFornat("yymmdd").

I'm wondering is there a way to force a four digit year format using the SimpleDateFormat? I'm close to do a regexp test upfront but maybe there is a smart way to use the (Simple)DateFormat()?

As requested the code, things are getting more complicate and my research was half. The Format used was yyyy-MM-dd to start with (it came from a variable, which had a wrong javadoc). However as indicated in an answer below yyyyMMdd does force a four year digit. So my question is changed to How to force a four digit year for the "yyyy-MM-dd" format. And why does "yyyyMMdd" behave different?

    public void testMaturity() {
   开发者_如何学C try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        System.out.println(" " + sdf.format(sdf.parse("40-12-14")));
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        sdf.setLenient(false);
        System.out.println(" " + sdf2.format(sdf2.parse("401214")));
        fail();
    } catch (ParseException pe) {
        assertTrue(true);
    }

Which prints 0040-12-14


Simply use yyyyMMdd (note: upper case M is used to indicate month, otherwise you're parsing minutes!) and then check if the year is greater some cutoff date (for example, when parsing birth dates, greater 1800 is a safe bet, when parsing dates for upcomming dates greater than or equal the current year would be good).


Hmm. I suspect you should be using "MM" instead of "mm" to start with... but "100624" doesn't parse anyway when I try it - even in lenient mode:

import java.util.*;
import java.text.*;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        format.setLenient(true);
        tryParse(format, "100624");
        format.setLenient(false);
        tryParse(format, "100624");
    }

    static void tryParse(DateFormat format, String text)
    {
        try
        {
            Date date = format.parse(text);
            System.out.println("Parsed " + text + " to " + date);
        }
        catch (ParseException pe)
        {
            System.out.println("Failed to parse " + text);
        }
    }
}

(And even using "mm" instead of "MM" it still fails to parse.)

Prints:

Failed to parse 100624
Failed to parse 100624

Perhaps you could show the code which is managing to parse this?


There is no easy way for this. If you're saying that dates can be like 1970 the main question would be what 700624 means - 1970 or 2070? You should either implement some cutoff date like Joachim proposed or move entirely to 4 digits year.


Well it is quite straight and forward. Follow these simple steps:

Step 1:

try {
    String dates = "40-12-14";
    String patterns = "yy-MM-dd";
    SimpleDateFormat format3 = new SimpleDateFormat(patterns);``
    java.util.Date dates1 = format3.parse(dates);

    SimpleDateFormat sdfDates = new SimpleDateFormat("dd/MM/yyyy");

    String strDate1 = sdfDate.format(dates1);
    System.out.println("Date1: " + strDate1);

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

This will output:

Date1: 14/12/1940
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜