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
The problem is that you're using 'yyyy-mm-dd' which pulls the year-minute-day. Instead use 'yyyy-MM-dd'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With