2

I want to get yesterday's date using java. I have used the following code but it is giving different date each time, Please check whether the code has to be change anywhere. Thanks in advance.

 SimpleDateFormat formatter= 
        new SimpleDateFormat("yyyy-mm-dd ");
    Calendar currentDate = Calendar.getInstance();
    String previous = formatter.format(currentDate.getTime())+ "00:00:00.000000000";
    System.out.println("previous ="+previous);
    currentDate.add(Calendar.DATE, -1);
    String previousDate = formatter.format(currentDate.getTime())+ "00:00:00.000000000";
    Timestamp updateTimestamp = Timestamp.valueOf(previousDate);
    System.out.println("Update date ="+updateTimestamp);

This is the output i got when i ran lastly
previous =2010-15-11 00:00:00.000000000
Update date =2011-03-10 00:00:00.0

raja
  • 39
  • 1
  • 1
  • 3

4 Answers4

15

The problem is that you're using 'yyyy-mm-dd' which pulls the year-minute-day. Instead use 'yyyy-MM-dd'.

SOA Nerd
  • 943
  • 5
  • 12
  • 1
    And for an overview of all valid patterns, just check `SimpleDateFormat` javadoc: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html – BalusC Feb 11 '10 at 13:16
10

You used mm in your pattern, so you're using minutes instead of months.

If you wanna use Joda Time, a simpler date framework, you could do the following :

DateTimeFormat format = DateTimeFormat.forPattern("yyyy-MM-dd 00:00:00.000000000");
DateTime now = new DateTime();
System.out.println("Previous :" + format.print(now);
DateTime oneDayAgo = now.minusDays(1);
System.out.println("Updated :" + format.print(oneDayAgo);
Valentin Rocher
  • 11,667
  • 45
  • 59
  • 1
    So `DateTime oneDayAgo = now.minusDays(1);` is so much less crappy than `currentDate.add(Calendar.DATE, -1);` that you advocate adding a 3rd party library just for this? – jarnbjo Feb 11 '10 at 13:09
  • 1
    No. But Joda Time, in general, takes care of all the little pitfalls the Java implementation on Date is full. It's just a good practice of using it if your're playing with dates. I'm not a JodaTime integrist, just wanted to give an alternate solution. – Valentin Rocher Feb 11 '10 at 13:13
  • I should add I voted for the current best solution, indicating what was false in his example. Joda Time helped me so much on trivial date problems with Java, so I just wanted to share it :) – Valentin Rocher Feb 11 '10 at 13:21
  • @Valentin: The 'pitfall' raja fell into was the difference between the formatting patterns mm and MM. How is JoDa taking care of this problem? – jarnbjo Feb 11 '10 at 13:21
  • As I said, I understood, my solution (changing libs) was a bit overkill, and I didn't read really well the question. Could we move on to another subject, or am I going to suffer an eternity of torment for suggesting Joda Time ? – Valentin Rocher Feb 11 '10 at 14:17
  • using joda time would not have solved the problem because the user used mm in stead of MM – Peter Feb 11 '10 at 14:30
  • 1
    ok, added the good answer, now could everyone stop telling me the same thing again and again ? – Valentin Rocher Feb 12 '10 at 02:16
6

Your date format pattern string is wrong. "mm" is minutes, "MM" is months. You could have solved this easily by looking at the intermediate results looking like "2010-52-11...".

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DATE, false); //you can also use add(int, int)
System.out.println(cal.toString());

All in standard Java since 1.1. Also have a look at GregorianCalendar if you need to. Read the docs to see how it handles daylight savings etc.

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
Cogsy
  • 5,584
  • 4
  • 35
  • 47
  • Grabbing `GregorianCalendar` separately is unnecessary. The `Calendar#getInstance()` already returns it. – BalusC Feb 11 '10 at 13:17
  • True, given that GregorianCalendar is the 'only' implementation of Calendar in standard Java. The docs might be useful to read through, however. – Cogsy Feb 11 '10 at 13:28
  • Cogsy: GregorianCalendar is the only Calendar implementation with a public API. Sun's Java 6 include implementations for the Japanese and Buddhist (Thai) calendars as well. – jarnbjo Feb 11 '10 at 13:36