Crutch with the Calendar in Java - java

I'm writing an app for android. On the start of it I create a Calendar, filling such containers as YEAR, MONTH, DAY_OF_MONTH, ERA and time zone as GMT.
I get something like this
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2021,MONTH=3,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=14,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]
Later on I need to get a string like "14-04-2021", so I create a Date variable using getTimeInMillies for the Calendar before. All the fields of it become filled with zeros.
After parsing a JSON, I need to create a map with Calendar keys. It looks like this.
String key = keys.next();
Date d = dateFormat.parse(key);
String rate = String.valueOf(rates.getJsonObject(key).get(get_curr_inf(get_curr_to_r())));
Double rate_ = Double.parseDouble(rate);
Calendar cal = new GregorianCalendar();
cal.setTime(d);
Calendar tmp = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
tmp.setTimeZone(TimeZone.getTimeZone("GMT"));
tmp.set(Calendar.ERA, 1);
long f = tmp.getTimeInMillis();
data.put(tmp, rate_);
I do it in such a strange way as it needs to be exactly similar to the previous Calendar, cause i parse map using it.
Is there any way to make it look better?

Never use Calendar. That terrible class was supplanted years ago by the modern java.time classes.
For a date-only value without time-of-day and without time zone, use LocalDate.
On the start of it I create a Calendar, filling such containers as YEAR, MONTH, DAY_OF_MONTH, ERA and time zone as GMT.
LocalDate ld = LocalDate.of( 2021, Month.APRIL , 14 ) ;
I need to get a string like "14-04-2021"
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
String output = ld.format( f ) ;
I need to create a map with Calendar keys
Map< LocalDate , … > map = new … ;
map.put( ld , … ) ;
The java.time classes are built into Android 26 and later. The latest Android tooling brings much of that functionality to earlier Android by way of API desugaring.

java.time
The first answer is to change your map to use LocalDate as key instead of Calendar. Two LocalDate objects denoting the same day will always be equal and produce the same hash code, so are ideal as map keys. Follow the answer by Basil Bourque.
The second answer, if for some reason you cannot change the type of the map keys, you can still produce an old-fashioned Calendar object more nicely using java.time, the modern Java date and time API.
String key = "14-04-2021";
LocalDate date = LocalDate.parse(key, DATE_FORMATTER);
ZonedDateTime zdt = date.atStartOfDay(ZoneOffset.UTC);
GregorianCalendar tmpCal = GregorianCalendar.from(zdt);
System.out.println(tmpCal);
I used this formatter:
private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("dd-MM-uuuu");
And output is:
java.util.GregorianCalendar[time=1618358400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2021,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=104,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
It is not completely equal to the result you had, so will not be compatible with Calendar objects produced your old way. One difference is that all fields are set. java.time cannot produce a Calendar with only selected fields set as you had before. The other difference could be eliminated: my time zone is UTC instead of GMT. In practice they are the same. To get GMT, use ZoneId.of("GMT") instead of ZoneOffset.UTC.
So if you can enforce that now all map keys are produced in the new way, I should say that you are set.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java 8+ APIs available through desugaring
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

Related

Exception parsing string to Date

I cannot see why this code is failing:
#Test
public void test() {
final SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmm");
try {
sdf.setLenient(false);
Date d = sdf.parse("202003290230");
} catch (ParseException e){
e.printStackTrace();
}
}
I get the exception java.text.ParseException: Unparseable date.
If I set sdf.setLenient(true) then it works, but the time in the object Date returned is '03:30' and not '02:30'.
Can someone explain me what's going on in here? Thanks.
As OH GOD SPIDERS noted in a comment your real problem is that that time doesn’t exist in you default time zone because of the spring forward, the transition to summer time (DST).
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work. The short-sighted answer is: use LocalDateTime from java.time.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMddHHmm");
String dateTimeString = "202003290230";
LocalDateTime ldt = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(ldt);
Output is:
2020-03-29T02:30
A LocalDateTime is a date and time without time zone. So it doesn’t discover that the time doesn’t exist in your time zone. The result is 2:30 as in the string.
Detecting a non-existing time
It seems you’re in a Central European time zone or some other time zone where summer time began on the last Sunday in March, and the clocks were turned forward from 2 to 3 (AM). So there was no 2:30. Supposing that you want to know, you may do:
ZoneId zone = ZoneId.of("Europe/Paris");
ZonedDateTime zdt = ldt.atZone(zone);
if (zdt.toLocalDateTime().equals(ldt)) {
System.out.println("The time " + zdt + " exists");
} else {
System.out.println("The time " + ldt + " does not exist in " + zone);
}
The time 2020-03-29T02:30 does not exist in Europe/Paris
java.time too gives you 3:30 instead of 2:30 when given a time in the spring gap. So when converting back from ZonedDateTime to LocalDateTime in this case we don’t get the same time again, which we use for detecting the non-existing time.
I further recommend that you use ZonedDateTime for past dates and times, not LocalDateTime.
Java 6?
This project is made with Java6 and the company doesn't want to update
it, …
java.time has been backported, and I have tested the code above with the backport, ThreeTen Backport, see the link at the bottom.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310). I’m unsure whether the very latest release of ThreeTen Backport works with Java 6 or only with Java 7(+). If this is an issue, go back a few releases and find one that works with Java 6 too.
On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Don’t use SimpleDateFormat
The date and time classes you were trying to use, Date and SimpleDateFormat, are poorly designed and long outdated. In your case they showed a behaviour that depends on a time zone that isn’t present in the code at all, which is quite confusing. I recommend that you don’t use those classes.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java 8+ APIs available through desugaring
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

DateTime("2019-02-16T10:00:00+08:00") conversion to local date time with offset value

I get the datetime content in the below string format with offset time value from the source system.
2019-02-16T10:00:00+08:00
Where i want to convert that into local date time using the offset value.I tried the below, but not getting the expected result.
DateTime date = new DateTime("2019-02-16T10:00:00+08:00");
-->output == 2019-02-16T02:00:00.000Z (the hour is decreased instead of increasing)
DateTime date = new DateTime("2019-02-16T10:00:00-08:00");
-->output == 2019-02-16T18:00:00.000Z (the hour is increased instead of decreasing).
is there any simple way to the expected output?
Note: I am using Java 1.7
What you are doing is correct. To get the time in your local time zone:
DateTime date = new DateTime("2019-02-16T10:00:00+08:00");
DateTime dateTimeInLocalTimeZone = date.withZone(DateTimeZone.getDefault());
System.out.println(dateTimeInLocalTimeZone);
On my computer in Europe/Copenhagen time zone I got
2019-02-16T03:00:00.000+01:00
As has been said in the comments, +08:00 is the offset that has already been added compared to UTC time. So your string denoted the same point in time as 2019-02-16T02:00:00+00:00. It may also be written as 2019-02-16T02:00:00Z since Z (pronounced “Zulu”) means UTC.
java.time and ThreeTen Backport
If you are not already tied to Joda-Time, you may prefer to use java.time, the modern Java date and time API. The code is similar:
OffsetDateTime sourceDateTime = OffsetDateTime.parse("2019-02-16T10:00:00+08:00");
ZonedDateTime dateTimeInLocalTimeZone = sourceDateTime.atZoneSameInstant(ZoneId.systemDefault());
2019-02-16T03:00+01:00[Europe/Copenhagen]
Question: Can I use java.time on Java 1.7?
Note: I am using Java 1.7
No big problem, java.time just requires at least Java 6. I have run the above code on jdk1.7.0_79.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Another way to do that :
String dt = "2019-02-16T10:00:00+08:00";
ZonedDateTime zd = ZonedDateTime.parse("2019-02-16T10:00:00+08:00");
System.out.println(zd.toLocalDateTime().plusSeconds(zd.getOffset().getTotalSeconds()));
Output
2019-02-16T18:00

Parsing a string to a datetime does not return the correct time

In Android...I am expecting 3:12 pm as time out put of the following code but I get 4:12 pm. Whats the correct way to parse this date time format.
String dt = "2018-09-02T19:12:00-0400";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = dateFormat.parse(dt);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
Time zone
It’s best to specify explicitly in which time zone you want your output:
DateTimeFormatter inputFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX");
DateTimeFormatter displayFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.ENGLISH);
ZoneId displayZone = ZoneId.of("Pacific/Pitcairn");
String dt = "2018-09-02T19:12:00-0400";
OffsetDateTime dateTime = OffsetDateTime.parse(dt, inputFormatter);
String displayDateTime = dateTime.atZoneSameInstant(displayZone)
.format(displayFormatter);
System.out.println(displayDateTime);
This prints:
September 2, 2018 at 3:12:00 PM PST
I have used Pacific/Pitcairn time zone in my code, but you know better which time zone you want.
I am also using java.time, the modern Java date and time API. The date-time classes you are using, SimpleDateFormat and Date, are considered long outdated, and java.time is so much nicer to work with.
What went wrong in your code?
Your way of parsing your date string is correct and produces the correct Date.
When printing the Date, you are implicitly calling toString. The outdated Date class has a peculiar and confusing toString method: it grabs the JVM’s time zone setting and uses it for producing the string. So depending on your default time zone, you can get any hour of day in the output. So it seems your JVM’s time zone setting didn’t correspond to what you had expected.
Since you expected 3:12 PM from your input of 19:12:00-0400, I take it that you want a time zone that is at offset -08:00 from UTC in September. If for example your default time zone was America/Los_Angeles, the standard time of which is at -08:00, you would get Sun Sep 02 16:12:00 PDT 2018 because summer time (daylight saving time) is in effect in California in September, so the offset is -07:00.
Relying on your JVM’s default time zone is always fragile since the setting may be changed at any time by other parts of your program or by other programs running in the same JVM.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

Not able to convert the string to date on Android

I have obtained a string from an API in form of 2017-04-23T19:47:39+00:00
How do i convert it into date of format "2017-04-23T19:47:39-0000"?
I tried the following but it gave ParseException
String created_at_string = "2017-04-23T19:47:39+00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date created_at = sdf.parse(created_at_string);
Exception:
06-22 18:18:32.517 10396-10396/com.harit.abs W/System.err: java.text.ParseException: Unparseable date: ""2018-04-29T10:55:37+00:00""
at java.text.DateFormat.parse(DateFormat.java:358)
at com.harit.abs.Sync_Activity.bu(Sync_Activity.java:169)
at com.harit.abs.Sync_Activity$2.onCompleted(Sync_Activity.java:118)
at com.harit.abs.Sync_Activity$2.onCompleted(Sync_Activity.java:87)
at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:246)
at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:60)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
Tried what is given in SimpleDateFormat with TimeZone
Still getting the same parse Exception
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
String created_at_string = "2017-04-23T19:47:39+00:00";
Date created_at = sdf.parse(created_at_string);
You don’t need to
The conversion you are asking about could easily be superfluous. You are asking for a string like 2017-04-23T19:47:39-0000. This string is in ISO 8601 format, the international standard format. So depending on your exact situation I would expect a string in ISO 8601 to be fine for you. The string you already got, 2017-04-23T19:47:39+00:00 is in ISO 8601 too! The standard allows for some variations, and one of them is that the colon in the offset is optional. So the first thing I think that you should do is to pass on the string you get and see if that works. In case it doesn’t, read on.
java.time
Your desired conversion goes so smoothly with java.time the modern Java date and time API:
DateTimeFormatter formatterWithoutColon
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxx");
String createdAtString = "2017-04-23T19:47:39+00:00";
OffsetDateTime dateTime = OffsetDateTime.parse(createdAtString);
String inDesiredFormat = dateTime.format(formatterWithoutColon);
System.out.println(inDesiredFormat);
This prints the desired:
2017-04-23T19:47:39+0000
Please note that we didn’t even need a formatter for parsing the string. OffsetDateTime and many other classes of java.time parse ISO 8601 as their default. Since our requirements for the result are a bit more precise, we use a formatter here. xx gives us the offset without colon, for example +0000.
The Date class that you used is long outdated. So is SimpleDateFormat, and it’s notoriously troublesome too. I suggest you avoid those classes.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
The hack
The change you require is very simple, you just want a colon removed. So another, not so nice, but basic and simple solution that doesn’t require ThreeTenABP is a regular expression:
String inDesiredFormat = createdAtString.replaceFirst("([+-]\\d{2}):(\\d{2})$", "$1$2");
This gives the same result as above.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.timeto Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
Possibly you could try to set the locale,
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

Generating Datetime sequence using Joda-Time API

I want to generate a sequence of time in the following format (yyyy-mm-dd hh:mm:ss) with time interval of 15 min. I will give start date and end date.
Used the below code to test the same.
public static void main(String[] args) throws ParseException {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd HH:mm:ss");
DateTime dt1 = formatter.parseDateTime("2017-06-21 00:00:00");
DateTime dt2 = formatter.parseDateTime("2017-06-23 00:00:00");
DateTime dateTime1 = new DateTime(dt1);
DateTime dateTime2 = new DateTime(dt2);
List<Date> allDates = new ArrayList();
while( dateTime1.isBefore(dateTime2) ){
allDates.add( dateTime1.toDate() );
dateTime1 = dateTime1.plusMinutes(15);
System.out.println(dateTime1);
}
It generates output like below:
2017-01-21T00:15:00.000+05:30
Expected output is 2017-06-21 00:00:00 , it's not picking up the right date which I wanted.
tl;dr
Use java.time classes, which supplant the Joda-Time project. Convert input to standard format for easy parsing.
LocalDateTime.parse(
"2017-06-23 00:00:00".replace( " " , "T" )
).plus( Duration.ofMinutes( 15 ) )
Details
As noted by JB Nizet, you are using incorrect codes in your formatting pattern. Apparently you are guessing at the codes rather reading the documentation – an unwise practice. These codes have been covered hundreds of times on Stack Overflow, so apparently you are posting here without bothering to first search.
Joda-Time vs java.time
You are using the Joda-Time library. This project is now in maintenance mode, with the team advising migration to the java.time classes.
ISO 8601 standard
Your input strings nearly comply with the ISO 8601 standard. To fully comply, replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to define any formatting pattern.
Caveat about LocalDateTime
Your input lacks any indication of time zone or offset-from-UTC. So we will parse as LocalDateTime. Beware that a LocalDateTime is not a real moment, not an actual point on the timeline. Without the context of a time zone or offset, a LocalDateTime is unrealistic.
Solution
LocalDateTime start = LocalDateTime.parse( "2017-06-21 00:00:00".replace( " " , "T" ) ) ;
LocalDateTime stop = LocalDateTime.parse( "2017-06-23 00:00:00".replace( " " , "T" ) ) ;
For defensive programming, verify your start is before your stop. Something like stop.isAfter( start ).
On each loop, add your specified duration of 15 minutes.
Duration d = Duration.ofMinutes( 15 ) ;
LocalDateTime ldt = start ;
List< LocalDateTime > ldts = new ArrayList<>() ;
while( ! ldt.isAfter( stop ) ) { // "Not after" is a shorter way of saying "is earlier than or is equal to".
ldts.add( ldt ) ;
// Set up the next loop.
ldt = ldt.plus( d ) ;
}
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
First of all, read the javadoc. The lowercase mm pattern corresponds to the minutes. To get the months, you need to use uppercase MM. So, your formatter will be like this:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
And you don't need to do this:
DateTime dateTime1 = new DateTime(dt1);
It's redundant, because you're creating 2 identical objects. Just use the dates returned by parseDateTime method. Or just do:
DateTime dateTime1 = dt1;
And you've already created a DateTimeFormatter, so just use it to format the output as well. Instead of:
System.out.println(dateTime1);
Do this:
System.out.println(formatter.print(dateTime1));
The print method will return the dates in the format you want.
I'm not sure if you wanted to print the first date (2017-06-21 00:00:00). If you want this, just change the order of the plusMinutes and System.out.println lines.
New Java Date/Time API
Joda-Time is in maintainance mode and is being replaced by the new APIs, so I don't recommend start a new project with it. Even in joda's website it says: "Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
So, if you can migrate your Joda-Time code, or starting a new project, consider using the new API. If you're using Java 8, you can use the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.
The logic is very similar. The only difference is that I used LocalDateTime class, and to convert it to a java.util.Date I need to know in what timezone it is. I used the system's default timezone, which is probably what you want (as your original code also uses the default timezone):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime1 = LocalDateTime.parse("2017-06-21 00:00:00", formatter);
LocalDateTime dateTime2 = LocalDateTime.parse("2017-06-23 00:00:00", formatter);
List<Date> allDates = new ArrayList();
while (dateTime1.isBefore(dateTime2)) {
// get the date in the system default timezone and convert to java.util.Date
allDates.add(Date.from(dateTime1.atZone(ZoneId.systemDefault()).toInstant()));
dateTime1 = dateTime1.plusMinutes(15);
System.out.println(formatter.format(dateTime1));
}
In Java 8 the Date.from method is available. In ThreeTen Backport (Java 7 and Android), you can use the org.threeten.bp.DateTimeUtils class instead:
allDates.add(DateTimeUtils.toDate(dateTime1.atZone(ZoneId.systemDefault()).toInstant()));

Categories

Resources