-4

my code is

final long MILLIS_IN_A_DAY = 1000*60*60*24;
Date yesterdaydate= new java.sql.Date(new java.util.Date().getTime() - MILLIS_IN_A_DAY);
out.println(yesterdaydate);

the output am getting is in format yyyy/mm/dd..how can i get it in MM/dd/yyyy

halfe
  • 85
  • 1
  • 2
  • 9
  • 1
    iQué horror! Please don't *ever* write code like this in production software! As far as "date differences" in Java, please consider using [Java 8](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html), or [Joda Time](http://www.joda.org/joda-time/): http://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime. As far as "date formatting", just use [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). – paulsm4 Jan 06 '16 at 04:43
  • 1
    Please don't ever assume there are exactly 24 hours in a day. Most places have a 25 hour day once per year, and a 23 hour day once per year. – Dawood ibn Kareem Jan 06 '16 at 04:49
  • 1
    @paulsm4 [`java.text.SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) is not applicable if using Java 8 or Joda-Time. They each have their own formatters ([`java.time.format.DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) and [`org.joda.time.format.DateTimeFormatter`](http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormatter.html)). – Andreas Jan 06 '16 at 08:32

3 Answers3

2

Please don't do like that . Get the date Using Date Object and parse it Using SimpleDateFormat . its Simple . Don't Go For Hard Coding the value. its Not Convenient for programmer . please find Code

Date date  = new Date();//Today Date
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");// FOrmat in This Format or you change Change as well 
String Format= format.format(date);
System.out.println(Format);// 01/06/2016

or

Calendar TodayDate = Calendar.getInstance();
TodayDate.add(Calendar.DAY_ON_MONTH, -1);//import java.util.*
Date yesterday = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String Format = format.format(yesterday);
System.out.println(Format);// 01/06/2016
Anand Dwivedi
  • 1,452
  • 13
  • 23
  • 3
    No need to write `"" +` in the calls to `println`. You mean `DAY_OF_MONTH`. And almost everyone in the world uses lower case for the first letter of a variable name, and your code will therefore be more universally readable if you do the same. – Dawood ibn Kareem Jan 06 '16 at 05:20
  • but here we get the formatted output as String, how to get the format("MM/dd/yyyy") as date type itself. – jeswin Mar 26 '20 at 07:25
1

To get yesterdays date and format it, you have 3 choices:

// Use Java 8+ LocalDate
java.time.LocalDate yesterday = LocalDate.now().minusDays(1);

String text = yesterday.format(DateTimeFormatter.ofPattern("MM/dd/uuuu"));
// Use Joda-Time (3rd-party library)
org.joda.time.LocalDate yesterday = LocalDate.now().minusDays(1);

String text = yesterday.toString("MM/dd/yyyy");
// Use Calendar (all Java versions, no 3rd-party library)
java.util.Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_ON_MONTH, -1);
java.util.Date yesterday = cal.getTime();

String text = new SimpleDateFormat("MM/dd/yyyy").format(yesterday);

Note that Calendar version still produces a "date" value with time-of-day. Extra code would be needed to clear the time fields.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String DateToStr = format.format(yesterdaydate);
out.println(DateToStr);
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • @Buwaneka Sumanasekara - your example is perfectly self-explanatory ... and, as far as I can see, correct. +1. It's still worth pointing out that the OP's "date diff" method is *WRONG*. I'd recommend the new Java 8 date/time functions, Joda time, or - as the others pointed out - Calendar.add(). – paulsm4 Jan 06 '16 at 06:04