user10267781
user10267781

Reputation:

Java String to Date Conversions to print 1 year of Dates

Code I have to get all dates for the past year start from the sDate1 string. This is code going nowhere right now but just added it, hope someone can help. There are string to LocalDateTime, Date to LocalDateTime error arising which I am not able to solve and its stuck

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");  
String Date = "20181120";
Date date1=new SimpleDateFormat("yyyyMMdd").parse(sDate1);

LocalDateTime start =LocalDateTime.now();
LocalDateTime end = start.minusYears (1);
List<String> First = new ArrayList<>();
List<String> Second = new ArrayList<>();
List<String> Third = new ArrayList<>();
while (!end.isAfter(start)) {
    First.add(dtf.format(end)+"_*_A.dat");
    Second.add(dtf.format(end)+"_*_B.dat");
    Third.add(dtf.format(end)+"_*_C.dat");
    end = end.plusDays(1);
}

Upvotes: 1

Views: 86

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339372

Stream

Java 9 and later added the LocalDate::datesUntil method to generate a stream of LocalDate objects.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ; 
Stream< LocalDate > stream = today.minusYears( 1 ).datesUntil( today ) ;

Loop through that stream.

stream.forEach( ( LocalDate localDate ) -> {
    System.out.println( localDate );
} );

2017-12-01

2017-12-02

2017-12-03

For more info on streams, see Oracle Tutorial and the articles, Processing Data with Java SE 8 Streams, Part 1 and Part 2.

Upvotes: 4

Anonymous
Anonymous

Reputation: 86324

  1. I thought your own code was good enough to go into an answer.
  2. I also though Basil Bourque’s suggestion of a stream was good enough to be spelled out a little more.

Your own answer

The code you posted in a comment is quite unreadable, of course. Here I have formatted it (or my Eclipse has):

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
    String sDate1 = "20181120";
    LocalDate date1 = LocalDate.parse(sDate1, dtf);
    LocalDate end = date1.plusYears(1);
    ArrayList<String> first = new ArrayList<>();
    ArrayList<String> second = new ArrayList<>();
    ArrayList<String> third = new ArrayList<>();
    while (!date1.isAfter(end)) {
        first.add(dtf.format(date1) + "*_A.dat");
        second.add(dtf.format(date1) + "_B.dat");
        third.add(dtf.format(date1) + "__C.dat");
        date1 = date1.plusDays(1);
    }

    // Let’s also see some of the result
    first.forEach(System.out::println);

Output, abbreviated:

20181120*_A.dat
20181121*_A.dat
20181122*_A.dat
20181123*_A.dat
…
20191118*_A.dat
20191119*_A.dat
20191120*_A.dat

To sum up the changes from the question:

  • You needed to parse the date string to get a start date. However there was no reason to bring in the troublesome and outdated SimpleDateFormat for that. java.time provides all the functionality we need.
  • Since we only need a date, not the time of day, there was also no reason to use LocalDateTime.
  • You probably still intended _*_ in front of each of A, B and C, but you can fix that yourself.

Use a stream

    List<String> first = date1.datesUntil(end.plusDays(1))
            .map(d -> d.format(dtf) + "_*_A.dat")
            .collect(Collectors.toList());

In this case I recommend three separate stream pipelines for the three lists. Since the datesUntil method accepts an exclusive end date and you want your end date included, we need to add 1 day.

Upvotes: 1

tschomacker
tschomacker

Reputation: 804

Not sure if I understood your specific task. But here is what I would write:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");  
String sDate1 = "20181120";
LocalDate date1= LocalDate.parse(sDate1, dtf);

LocalDate start = LocalDate.now();
LocalDate end = start.plusYears(1);
ArrayList<String> first = new ArrayList<>();
ArrayList<String> second = new ArrayList<>();
ArrayList<String> third = new ArrayList<>();
while (!start.isAfter(end)) {
    first.add(dtf.format(start)+"_*_A.dat");
    second.add(dtf.format(start)+"_*_B.dat");
    third.add(dtf.format(start)+"_*_C.dat");
    start = start.plusDays(1);
}

Upvotes: 0

Related Questions