Reputation: 19582
Is it possible to define a format that combines 2 dates?
E.g. assume I have date1
e.g. 15/1/2016 and date2
. e.g. 20/1/2016
Is there any format that I could use to show something like:
15-20 Jan 2016 ?
Other example:
E.g. assume I have date1
e.g. 15/1/2016 and date2
. e.g. 20/5/2016
Result: 15 Jan - 20 May 2016
Upvotes: 0
Views: 174
Reputation: 339362
The ISO 8601 standard defines formats for textual representations of various date-time values.
The standard way to represent a span of time is with the pair of dates formatted as YYYY-MM-DD, conjoined by a slash character.
2016-01-15/2016-05-20
LocalDate
Using the LocalDate
class in the java.time framework built into Java 8 and later, with a pair of LocalDate
objects named start
and stop
.
String output = start.toString() + "/" + stop.toString();
Upvotes: 0
Reputation: 3189
public String dateRangeToString(Date date1, Date date2){
Calendar firstCal = Calendar.getInstance();
firstCal.setTime(date1);
Calendar secondCal = Calendar.getInstance();
secondCal .setTime(date2);
if (firstCal.getTimeInMillis() > secondCal.getTimeInMillis()){
firstCal.setTime(date2);
secondCal .setTime(date1);
}
String result;
SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
if (firstCal.get(Calendar.YEAR) < secondCal.get(Calendar.YEAR)){
result = format.format(firstCal.getTime()) + " - " + format.format(secondCal.getTime());
}
else if (firstCal.get(Calendar.MONTH) < secondCal.get(Calendar.MONTH)){
SimpleDateFormat formatTemp = new SimpleDateFormat("dd MMMM");
result = formatTemp .format(firstCal.getTime()) + " - " + format.format(secondCal.getTime());
}
else if (firstCal.get(Calendar.DAY_OF_MONTH) < secondCal.get(Calendar.DAY_OF_MONTH)){
SimpleDateFormat formatTemp = new SimpleDateFormat("dd");
result = formatTemp .format(firstCal.getTime()) + " - " + format.format(secondCal.getTime());
}
else{//Both dates are equal
result = format.format(firstCal.getTime());
}
return result;
}
That should work, not pretty, but since you can have so many variations, it should manage what you want.
To explain, it has to use an if statement to check what parts of the date are not equal, is it just the year, month or day? Depending on what part fails will dictate which string to output. I then use variations of the SimpleDateFormat
to output the desired string
EDIT Using the following code:
Calendar calendar = Calendar.getInstance();
calendar.set(2013,5,29);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2012,4,30);
System.out.println(dateRangeToString(calendar.getTime(), calendar2.getTime()));
calendar.set(2013,5,29);
calendar2.set(2013,4,30);
System.out.println(dateRangeToString(calendar.getTime(), calendar2.getTime()));
calendar.set(2013,5,29);
calendar2.set(2013,5,30);
System.out.println(dateRangeToString(calendar.getTime(), calendar2.getTime()));
calendar.set(2013,5,30);
calendar2.set(2013,5,30);
System.out.println(dateRangeToString(calendar.getTime(), calendar2.getTime()));
I get the following results:
30 May 2012 - 29 June 2013
30 May - 29 June 2013
29 - 30 June 2013
30 June 2013
Upvotes: 0
Reputation: 2989
15-20 May 2016 - this is not a true date format . i dont think that there is any format to do this. but you can define your own method which will return an string and if you are using java 8 use localDate
LocalDate myDate = new LocalDate("2016-5-15");
LocalDate endDate =new LocalDate(2016-5-20);
String format = mydate.getDayOfMonth()
+"-"+ endDate.getDayOfMonth()
+" "+endDate.getMonth().toString()
+" "+endDate.getYear();
Upvotes: 1
Reputation: 206896
Here is one way to do this. Ofcourse this does not take into account that the dates may be in different months or years. It takes the month and year from startDate
.
LocalDate startDate = LocalDate.of(2016, 5, 15);
LocalDate endDate = LocalDate.of(2016, 5, 20);
String text = String.format("%1$te-%2$te %1$tB %1$tY", startDate, endDate);
System.out.println(text);
See the documentation for how the format string works.
NOTE: This uses Java 8 java.time.LocalDate
and not Joda Time.
Upvotes: 3