0

I want to reduce one day for a particular date. But It's reducing for 1 month.

df = new SimpleDateFormat("EEEE, dd MMMM YYYY"); 
Date prevDate = df.parse(dateText.getText().toString()); 
Calendar c = Calendar.getInstance(); 
c.setTime(prevDate); 
c.add(Calendar.DATE, -1); 
Date d=c.getTime(); 
dateText.setText(df.format(d));
Lukas Novicky
  • 921
  • 1
  • 19
  • 44
Priyanka
  • 138
  • 2
  • 15
  • There is a problem with your date format it should be "dd MM YYYY" – Karan Mehta Feb 07 '20 at 06:21
  • 1
    Just change **YYYY** to **yyyy** in date format, rest is ok. Know more here; https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#year – Mudassir Feb 07 '20 at 06:29
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 07 '20 at 15:35

4 Answers4

3

java.time.LocalDate

You can use LocalDate as below,

        String dateText="Mon, 14 May 2018";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMMM yyyy", Locale.ENGLISH);
        LocalDate localDate = LocalDate.parse(dateText, formatter);
        localDate = localDate.minusDays(1);

Best to avoid Date class. But if you must, you can convert.

Date oneDayBefore = 
    Date.from(                       // New conversion method added to the old class.
        localDate
        .atStartOfDay(               // Determine the first moment of the day as seen in that time zone. Not always 00:00.
            ZoneId.systemDefault()
        )                            // Returns another `LocalDate` object.
        .toInstant()                 // Returns a `Instant`.
    )
;                                    // Only if you *really* need java.util.Date object.

Print results.

        System.out.println(oneDayBefore);
        String dateTextOneDayBefore = formatter.format(localDate);//Format to similar to input 'dateText'
        System.out.println(dateTextOneDayBefore);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Nagaraja JB
  • 729
  • 8
  • 18
  • 2
    Your use of `LocalDate` is great. Not sure why you go back to `Date` to print, though. You can use `formatter.format(localDate)` instead. Also, I recommend setting `Locale.ENGLISH` in the `DateTimeFormatter`, since you are parsing English text. – TiiJ7 Feb 07 '20 at 07:52
  • Thank you @TiiJ7. Edited and added Locale.ENGLISH. I wouldn't have converted to java.util.Date. If they want java.util.Date for whatever logic there after. – Nagaraja JB Feb 07 '20 at 08:57
  • Per the Comments, I edited your Answer to add a note about not recommending `Date`, yet showing how to convert if they must. – Basil Bourque Feb 08 '20 at 22:28
1

Replace

df = new SimpleDateFormat("EEEE, dd MMMM YYYY");

With

df = new SimpleDateFormat("EEEE, dd MMMM yyyy");

look at this http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. Suggesting their use in 2020 is poor advice. – Basil Bourque Feb 08 '20 at 22:25
0
    //variable to store date in string format
    String yesterdayDate=null; 

    //to get calendar instance 
    Calendar cal = Calendar.getInstance();

    //subtract 1 from calendar current date 
    cal.add(Calendar.DATE, -1);

    //format date
    DateFormat dateFormat = new SimpleDateFormat("EEEE, dd MM YYYY");

    //get formatted date
    yesterdayDate=dateFormat.format(cal.getTime());

    System.out.println("Yesterday's date = "+ yesterdayDate);  
Rakesh R
  • 695
  • 7
  • 18
0

If you insist of using Calendar, try the following:

Calendar c = Calendar.getInstance(); 
c.setTime(prevDate); 
c.add(Calendar.HOUR, -24); 

But you also use milliseconds. In that case maybe the following would be enough:

date.setTime(date.getTime() - 24 * 60 * 60 * 1000);

the best way to do it, would be to use JodaTime:

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);

I would use JodaTime, especially if you have more date/time operations in your app.

Lukas Novicky
  • 921
  • 1
  • 19
  • 44
  • 1
    Joda-Time is a great leap forward compared to the poorly designed and long outdated `Calendar` class. Still better is [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and the successor of Joda-Time. – Ole V.V. Feb 07 '20 at 20:31
  • 1
    you are right. My answer assumes that there is some reason for user to not using new Java date time api, like working on Java older than 8 – Lukas Novicky Feb 11 '20 at 10:35