225

So I get a date attribute from an incoming object in the form:

Tue May 24 05:05:16 EDT 2011

I am writing a simple helper method to convert it to a calendar method, I was using the following code:

    public static Calendar DateToCalendar(Date date ) 
{ 
 Calendar cal = null;
 try {   
  DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
  date = (Date)formatter.parse(date.toString()); 
  cal=Calendar.getInstance();
  cal.setTime(date);
  }
  catch (ParseException e)
  {
      System.out.println("Exception :"+e);  
  }  
  return cal;
 }

To simulate the incoming object I am just assigning the values within the code currently using:

private Date m_lastActivityDate = new Date();

However this is givin me a null pointer once the method reaches:

date = (Date)formatter.parse(date.toString()); 
Will
  • 8,246
  • 16
  • 60
  • 92
  • 1
    For anyone arriving here late: I recommend you neither use `Date` nor `Calendar`. Those classes are poorly designed and long outdated. Instead use `Instant` and `ZonedDateTime`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 26 '19 at 16:38

3 Answers3

516

Here's your method:

public static Calendar toCalendar(Date date){ 
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  return cal;
}

Everything else you are doing is both wrong and unnecessary.

BTW, Java Naming conventions suggest that method names start with a lower case letter, so it should be: dateToCalendar or toCalendar (as shown).


OK, let's milk your code, shall we?

DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
date = (Date)formatter.parse(date.toString()); 

DateFormat is used to convert Strings to Dates (parse()) or Dates to Strings (format()). You are using it to parse the String representation of a Date back to a Date. This can't be right, can it?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Many thanks man. I actually got that code of a website. Given the cleanness of the above method I am now begining to feel my intToCalendar method my be somewhat bloated ` public static Calendar intToCalendar(int i) {` Calendar cal = null; try { String stringInt = String.valueOf(i); DateFormat formatter = new SimpleDateFormat("yyyyMMdd"); Date date = (Date)formatter.parse(stringInt); cal=Calendar.getInstance(); cal.setTime(date); } catch (ParseException e) { System.out.println("Exception :"+e); } return cal; }` – Will May 31 '11 at 10:26
  • 1
    @Will a) don't use int, use long. Dates hold larger values than int can store. b) use [new Date(long)](http://download.oracle.com/javase/6/docs/api/java/util/Date.html#Date%28long%29) – Sean Patrick Floyd May 31 '11 at 10:31
  • 1
    It looks like the code in the original question was intended not only to convert `Date` to `Calendar`, but also to remove the time from the date at the same time. How to do the latter correctly can be found [here](http://stackoverflow.com/questions/5050170/java-getting-date-without-time). Instead of using `(Date)formatter.parse(date.toString());`, try `formatter.parse(formatter.format(date));` – Chris Mar 16 '13 at 11:59
  • I think the original code was trying to parse only the year-month-day of the formatted date so that the calendar would be set to the start of the day, not some time in the middle of the day. Cute way to (attempt to) do it, although `set(HOUR_OF_DAY, 0)` etc. is better. – Andy Sep 30 '13 at 18:10
25

Just use Apache Commons

DateUtils.toCalendar(Date date)

David Trujillo
  • 251
  • 3
  • 3
-7

it's so easy...converting a date to calendar like this:

Calendar cal=Calendar.getInstance();
DateFormat format=new SimpleDateFormat("yyyy/mm/dd");
format.format(date);
cal=format.getCalendar();
mico
  • 12,730
  • 12
  • 59
  • 99
jsina
  • 4,433
  • 1
  • 30
  • 28
  • 5
    The accepted solution from two years ago is a cleaner way to do this. Observe that this code has the hidden assumption that the calendar object associated with `format` is currently holding the date that we just formatted, and that as a side effect of formatting with a date having no hours/minutes/seconds, that internal date also has no hours/minutes/seconds. Maybe, but not documented behavior. Do what the accepted answer does, which directly says what we are trying to do. If you then want to remove the hour/minutes/seconds, see comment on the accepted answer about how to do that. – ToolmakerSteve Sep 06 '15 at 19:41