Reputation: 7788
I'm looking for the simplest way to create range of years that I can put into a list starting with a from year and ending with an end year.
Example
String fromYear = "2010"
String toYear = "2014"
expecting to create a list of years ["2010","2011","2012","2013","2014"]
No I know I need to convert the string to an Integer
Integer from = fromYear != null ? Integer.parseInt(fromYear) : null;
Integer to = toYear != null ? Integer.parseInt(toYear) : null;
From there I'm lost as for the most efficient way to complete this.
Upvotes: 5
Views: 3365
Reputation: 10843
Here's what it's like with the new Java 8 streams API. I am going to assume that there is no use case where either year could be null
, and will thus throw an error if one of them is:
String fromYear = "2010" ;
String toYear = "2014" ;
List<Integer> years =
IntStream.rangeClosed
(Integer.parseInt(Objects.requireNonNull(fromYear)),
Integer.parseInt(Objects.requireNonNull(toYear)))
.boxed()
.collect(Collectors.toList());
Steps correspond roughly to: ensure fromYear
and toYear
are both non-null
, else throw an exception; convert fromYear
and toYear
to integers; create a stream of int
values beginning with fromYear
and ending with toYear
; convert them into Integer
instances (that's boxed()
); and package them as a list.
Upvotes: 5
Reputation: 31514
You should not create such a specific function that takes strings, it's normally better to have a range function that takes ints and convert the strings using Integer.parseInt
as you already said in the question.
Then you can define your own function:
public static List<Integer> range(int from, int to) {
List<Integer> list = new ArrayList<Integer>();
for (int i = from; i <= to; ++i)
list.add(i);
return list;
}
Otherwise if you are using Java 8 take a look at IntStream.rangeClosed
. You could also use Guava's Range
, they are nice because using iterators, they don't have to physically instantiate the list.
Upvotes: 3
Reputation: 8956
Treat Date as a Date
String fromYear = "2010";
String toYear = "2014";
SimpleDateFormat df = new SimpleDateFormat("yyyy", Locale.ENGLISH);
List<String> listOfDates = new ArrayList();
Calendar startCal = Calendar.getInstance(Locale.ENGLISH);
startCal.setTime(df.parse(fromYear));
Calendar endCal = Calendar.getInstance(Locale.ENGLISH);
endCal.setTime(df.parse(toYear));
while (startCal.getTimeInMillis() <= endCal.getTimeInMillis()){
java.util.Date date = startCal.getTime();
listOfDates.add(new SimpleDateFormat("yyyy", Locale.ENGLISH).format(date).trim());
startCal.add(Calendar.YEAR, 1);
}
System.out.println(listOfDates);
Upvotes: 1
Reputation: 2296
Takes String
arguments, converts and returns Integer
list:
public static List<Integer> range(String startYear, String endYear) {
int cur = Integer.parseInt(startYear);
int stop = Integer.parseInt(endYear);
List<Integer> list = new ArrayList<Integer>();
while (cur <= stop) {
list.add(cur++);
}
return list;
}
Or if you need to return String
list:
public static List<String> range(String startYear, String endYear) {
int cur = Integer.parseInt(startYear);
int stop = Integer.parseInt(endYear);
List<String> list = new ArrayList<String>();
while (cur <= stop) {
list.add(String.valueOf(cur++));
}
return list;
}
Upvotes: 4