Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doubt in getting yesterday's date using java

Tags:

java

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

like image 235
raja Avatar asked Mar 21 '26 18:03

raja


1 Answers

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

like image 144
SOA Nerd Avatar answered Mar 24 '26 07:03

SOA Nerd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!