How to get previous 7 dates from a particular date in java?I am getting 7 dates from present date, but I want from particular date
Asked Answered
W

3

-1
//explain
public class DateLoop {
    static String finalDate; 
    static String particularDate;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SimpleDateFormat sdf = new SimpleDateFormat("d-M-yyyy ");
        Calendar cal = Calendar.getInstance();
        particularDate = "2-1-2018";
        // get starting date
        cal.add(Calendar.DAY_OF_YEAR, -7);

        // loop adding one day in each iteration
        for(int i = 0; i< 7; i++){
            cal.add(Calendar.DAY_OF_YEAR, 1);
            finalDate =sdf.format(cal.getTime());
            System.out.println(finalDate);
            //ie, its giving previous 7 dates from present date, but I want
            //particular date... thanks in advance
        }
    }

}

ie, its giving previous 7 dates from present date, but I want previous 7 dates from particular date.

Windhover answered 9/1, 2018 at 18:31 Comment(2)
Having a variable particularDate does nothing to the calendar instance. You have to actually set it to that date.Bluefield
In java 8 u can use LocalDateTime to achieve that easily.Read about it please.Thebaine
G
3

tl;dr

LocalDate.of( 2018 , Month.JANUARY , 23 )
         .minusDays( … )

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Use LocalDate for a date-only without time-of-day.

Using the Month enum.

LocalDate start = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;  // 2018-01-23.

Using month numbers, 1-12 for January-December.

LocalDate start = LocalDate.of( 2018 , 1 , 23 ) ;  // 2018-01-23.

Collect a sequence of dates.

List<LocalDate> dates = new ArrayList<>( 7 ) ;
for( int i = 1 ; i <= 7 ; i ++ ) {
    LocalDate ld = start.minusDays( i ) ;  // Determine previous date.
    dates.add( ld ) ;  // Add that date object to the list. 
}

For earlier Android, use the ThreeTen-Backport and ThreeTenABP projects.

Gandzha answered 9/1, 2018 at 20:27 Comment(0)
P
1

As Uta Alexandru and Basil Bourque have said already, don’t use the long outmoted classes SimpleDateFormat and Calendar. java.time, the modern Java date and time API also known as JSR-310, is so much nicer to work with:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-M-uuuu");
    LocalDate date = LocalDate.parse("2-1-2018", dtf)
            .minusDays(7);

    for(int i = 0; i < 7; i++) {
        date = date.plusDays(1);
        String finalDate = date.format(dtf);
        System.out.println(finalDate);
    }

This prints:

27-12-2017
28-12-2017
29-12-2017
30-12-2017
31-12-2017
1-1-2018
2-1-2018

Not only is the code slightly simpler and shorter, more importantly, it is clearer and more natural to read.

Question: Can I use java.time on Android?

You certainly can. It just requires at least Java 6.

  • In Java 8 and later the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310).
  • On Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP.

Links

Pirandello answered 9/1, 2018 at 20:34 Comment(6)
I am getting exception running in android studio:java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/format/DateTimeFormatter;Windhover
You’re on the right track. Change your import statements to import org.threeten.bp.format.DateTimeFormatter and org.threeten.bp.LocalDate (the classes from the backport instead of those from the java.time package).Pirandello
can you please tell me the dependencies to import org.threeten.bp?Windhover
You need the ThreeTenABP project, it’s all explained here: How to use ThreeTenABP in Android Project. Sorry, I don’t know more than it says there. @SATYAJITTARAFDARPirandello
Oh!! "SEXY" thanks a lot, I got from the sample project and its working perfectly.Windhover
dependencies is: compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'Windhover
K
0

if you want to have some date from some data do something like below.

public void dateFromRandomDate(String date){
    SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");  
    Date date2=formatter2.parse(date); 
    Calendar calendar = Calendar.getInstance();
    //this sets the date to given date
    calendar.calendar.setTime(date2);
    //now call getTime() or add ,subtract date from here
    //this will add 1 year to given one,similarlly others will work.
    calendar.add(Calendar.YEAR,1);
}
Kariekaril answered 9/1, 2018 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.