2

I have a string like "1512". I need to convert it to a date 2015-12-31 23:59:59.

In that case, I am using Java Dateformat parse.

My code:

private static final dateformat = new SimpleDateFormat("yyMM");

public static boolean checkDate(String date){
Date date =  dateformat.parse(date);
}

It can give date upto 20 years. When date is "3610", it gives, 1936, instead of 2036 (of the current century).

Zakir CSE
  • 23
  • 4
  • So you want to be able to parse a future date? Also, just curious, why do you pick the last possible second of the specified month? – Arc676 Nov 04 '15 at 11:23
  • [Surely nobody in their right mind still thinks its a good idea to write software that handles years as two-digit numbers?](https://en.wikipedia.org/wiki/Year_2000_problem) Can't you afford two extra bytes? – r3mainer Nov 04 '15 at 11:28
  • I am working in ISO 8583 date..The specification says date format is in YYMM format. – Zakir CSE Nov 04 '15 at 11:30
  • The `Date` class is outdated and badly designed, as is the associated `Calendar` class. It's recommended to manipulate dates and times using `java.time` in Java 8, or Joda Time in earlier versions of Java, and avoid using `Date` except when legacy code mandates it. – RealSkeptic Nov 04 '15 at 11:31
  • @RealSkeptic Thanks for the information, can you point out any discussion related to your point. It will be helpful. – rajuGT Nov 04 '15 at 11:32
  • We cannot use Java 8 in production environment. We need to use Java 7 1.7.51. – Zakir CSE Nov 04 '15 at 11:36
  • So get the [Joda-Time library](http://www.joda.org/joda-time/) – RealSkeptic Nov 04 '15 at 11:38
  • SimpleDateFormat is not thread-safe so don't reference it statically – Brian Agnew Nov 04 '15 at 11:42
  • Are you sure, JodaTime can solve the issue? So far I know, JodaTime is also depend on DateFormat. As long as DateFormat being used, it will be the same. – Zakir CSE Nov 04 '15 at 11:43
  • 1
    @rajuGT Take a look at [this question](http://stackoverflow.com/q/1969442/4125191) for the problems in the old Java Date API. – RealSkeptic Nov 04 '15 at 11:44
  • @ZakirCSE JodaTime has its own formatting classes, not `DateFormat`. Its [DateTimeFormatterBuilder](http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html), for example, allows you to set your own pivot year for two-digit years, etc. – RealSkeptic Nov 04 '15 at 11:49

3 Answers3

2

I think, you can manually parse the String and then create the Date object.

suppose:

public static void checkDate(String date) throws ParseException {
    Calendar calendar = Calendar.getInstance();
    int year = Integer.parseInt(date.substring(0, 2));
    int month = Integer.parseInt(date.substring(2, 4));
    calendar.setLenient(false);         
    int yearOfCentury = calendar.get(Calendar.YEAR);
    int century = yearOfCentury - yearOfCentury % 100;
    year = year + century;
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month-1);
    calendar.set(Calendar.DATE,             calendar.getActualMaximum(Calendar.DATE));
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE,  59);
    calendar.set(Calendar.SECOND,  59);
    System.out.println("Date +" + calendar.getTime());    
}
Saikat Biswas
  • 114
  • 1
  • 9
1

If you are sure that the year should be always 2000+ then prefix the string with "20" manually and use the SimpleDateFormat as

private static final dateformat = new SimpleDateFormat("yyyyMM");

From the docs:

For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964.

rajuGT
  • 6,224
  • 2
  • 26
  • 44
0

The default behaviour of the YearMonth parser in Java 8 for 2 digit years is to start from 2000. So this would give you the result you expect:

YearMonth ym = YearMonth.parse("3610", DateTimeFormatter.ofPattern("yyMM"));
//ym = 2036-10

If you want a cut-off at a specific date (say you want 80-99 to be 1980-1999 and 0-79 to be 2000-2079) you can use a custom pattern.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783