How to add days to java.sql.date? - java

Here is my program, I tried
java.sql.Date logicalDate;
Calendar c = Calendar.getInstance();
c.setTime(logicalDate);
c.add(Calendar.DATE, 1);
The line below is showing an error the constructor Date(date) is undefined
java.sql.Date startDate= new java.sql.Date(c.getTime());
How do I add 1 day to java.sql.Date logicalDate?

Calendar#getTime returns a java.util.Date representation of the Calendar. You really need to use Calendar#getTimeInMillis instead
java.sql.Date startDate= new java.sql.Date(c.getTimeInMillis())

Here is a method
private Date sqlDatePlusDays(Date date, int days) {
return Date.valueOf(date.toLocalDate().plusDays(days));
}

import java.sql.Date;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String a[]) {
java.sql.Date todaysDate = new java.sql.Date(new java.util.Date().getTime());
int futureDay =1;
int pastDay=2;
java.sql.Date futureDate = this.addDays(todaysDate, futureDay);
java.sql.Date pastDate = this.subtractDays(todaysDate, pastDay);
System.out.println("futureDate =>>> " + futureDate);
System.out.println("pastDate =>>> " + pastDate);
}
public static Date addDays(Date date, int days) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, days);
return new Date(c.getTimeInMillis());
}
public static Date subtractDays(Date date, int days) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, -days);
return new Date(c.getTimeInMillis());
}
}

tl;dr
Never use the terrible legacy classes java.sql.Date, java.util.Calendar, java.util.GregorianCalendar, and java.util.Date. Use only java.time classes.
How do I add 1 day to java.sql.Date logicalDate?
Convert from legacy class java.sql.Date to modern class java.time.LocalDate. Then call plus…/minus… methods to add/subtract days, weeks, months, or years.
myJavaSqlDate.toLocalDate().plusDays( 1 )
If given a Calendar that is actually a GregorianCalendar, and you want only the date, convert to ZonedDateTime and extract a LocalDate.
( (GregorianCalendar) myCal ) // Cast from general `Calendar` to more concrete class `GregorianCalendar`.
.toZonedDateTime() // Convert from the legacy class to the modern `ZonedDateTime` class replacement.
.toLocalDate() // Extract the date only, omitting the time-of-day and the time zone.
.plusWeeks( 1 ) // Returns a `LocalDate` object, a date without a time-of-day and without a time zone.
java.time
The badly designed java.sql.Date class was years ago supplanted by the modern java.time classes with the unanimous adoption of JSR 310. Avoid Calendar class too.
Retrieve from your database with JDBC 4.2 by calling ResultSet::getObject.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
Add a week by calling LocalDate::plusWeeks.
LocalDate weekLater = ld.plusWeeks( 1 ) ;
Write to database by calling PreparedStatement::setObject.
myPreparedStatement.setObject( … , weekLater ) ;
Best to avoid the troublesome legacy date-time classes entirely. But if you need a java.sql.Date object to interoperate with old code not yet updated to java.time, use new conversion methods added to the old classes. Specifically, java.sql.Date.valueOf.
java.sql.Date d = java.sql.Date.valueOf( ld ) ;
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.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.

try this :
Calendar cNow = Calendar.getInstance();
Date dNow = cNow.getTime();
cNow.add(Calendar.DATE, 7);
Date dSeven = cNow.getTime();
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
String dateNow = format.format(dNow);
String dayBefore = format.format(dSeven);
System.out.println(dateNow);
System.out.println(daySeven); //here is your current day + 7
Date date = new Date();
System.out.println(date);

Related

Add n number of days using simpledateformat in java

We have a java code snippet here
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
Date date = new Date();
int days = 5;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate= formatter.format(date.getTime() + (days*86400000));
System.out.println(strDate);
}
}
to add n no. of days to today's date. The result will be correct upto n=24 but gives previous month' after n=24. Why it is so?
The problem is the the int is overflowing
consider
int days = 25;
int d = days*86400000;
System.out.println(d);
try
int days = 25;
long d = days*86400000L;
System.out.println(d);
tl;dr
LocalDate // Represent a date-only, without a time-of-day and without a time zone.
.now() // Capture the current date, as seen through your JVM’s current default time zone. Better to pass a `ZoneId` as the optional argument.
.plusDays( 5 ) // Add five days, returning a new `LocalDate` object. Per the Immutable Objects pattern, a new object is produced rather than changing (“mutating”) the original.
.format( // Generate text representing the date value of our `LocalDate` object.
DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) // Define a formatting pattern to suit your taste. Or call the `.ofLocalized…` methods to localize automatically.
) // Returns a `String`.
java.time
Date class represents a moment in UTC, a date with a time-of-day, and an offset-from-UTC of zero. Wrong class to use when working with date-only values.
Avoid using the terrible old legacy date-time classes such as Calendar, Date, and SimpleDateFormat. These classes were supplanted years ago by the java.time classes.
Do not track days as a count of seconds or milliseconds. Days are not always 24 hours long, and years are not always 365 days long.
LocalDate
Instead, use LocalDate class.
LocalDate today = LocalDate.now() ;
LocalDate later = today.plusDays( 5 ) ;
Convert
Best to avoid the legacy classes altogether. But if you must interoperate with old code not yet updated to java.time classes, you can convert back-and-forth. Call new methods added to the old classes.
For Date you need to add a time-of-day. I expect you will want to go with the first moment of the day. And I'll assume you want to frame the date as UTC rather than a time zone. We must go through a OffsetDateTime object to add the time-of-day and offset. For the offset, we use the constant ZoneOffset.UTC. Then we extract the more basic Instant class object to convert to a java.util.Date.
OffsetDateTime odt = OffsetDateTime.of( later , LocalTime.MIN , ZoneOffset.UTC ) ; // Combine the date with time-of-day and with an offset-from-UTC.
Instant instant = odt.toInstant() ; // Convert to the more basic `Instant` class, a moment in UTC, always UTC by definition.
java.util.Date d = java.util.Date.from( instant ) ; // Convert from modern class to legacy class.
Going the other direction:
Instant instant = d.toInstant() ; // Convert from legacy class to modern class.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.
Use days*86400000L to make this a long calculation otherwise the int value overflows.
Try this one in your code:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 5);
strDate = formatter.format(cal.getTime());

Retrieve Month, Day and Year values from a String using Java

How to extract Day, Month and Year values from a string [like 18/08/2012]. I tried using SimpleDateFormat, but it returns a Date object and I observed that all the Get methods are deprecated. Is there any better way to do this?
Thanks
Personally I'd use Joda Time, which makes life considerably simpler. In particular, it means you don't need to worry about the time zone of the Calendar vs the time zone of a SimpleDateFormat - you can just parse to a LocalDate, which is what the data really shows you. It also means you don't need to worry about months being 0-based :)
Joda Time makes many date/time operations much more pleasant.
import java.util.*;
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) throws Exception {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy")
.withLocale(Locale.UK);
LocalDate date = formatter.parseLocalDate("18/08/2012");
System.out.println(date.getYear()); // 2012
System.out.println(date.getMonthOfYear()); // 8
System.out.println(date.getDayOfMonth()); // 18
}
}
Simply go for String.split(),
String str[] = "18/08/2012".split("/");
int day = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
..... and so on
This should get you going without adding external jars
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date parse = sdf.parse("18/08/2012");
Calendar c = Calendar.getInstance();
c.setTime(parse);
System.out.println(c.get(Calendar.MONTH) + c.get(Calendar.DATE) + c.get(Calendar.YEAR));
Create a java.util.Calendar object out of that date as follows and extract the date parts:
Calendar cal = Calendar.getInstance();
cal.setTime(<date from simple-date-format).
cal.get(Calendar.MONTH);
etc.,
tl;dr
java.time.LocalDate.parse(
"18/08/2012" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
).getDayOfMonth​() // .getYear​() .getMonth()
java.time
The modern approach uses the java.time classes. Avoid the troublesome legacy classes such as Date & Calendar.
LocalDate
String input = "18/08/2012" ;
The LocalDate class represents a date-only value without time-of-day and without time zone.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
ld.toString(): 2012-08-18
Getter methods
Interrogate for the parts.
int d = ld.getDayOfMonth​() ;
int m = ld.getMonthValue() ;
int y = ld.getYear() ;
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
Later versions of Android bundle implementations of the java.time classes.
For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.
Another approach may be use Calendar object get(Calendar.MONT)
Example:
Calendar cal = Calendar.getInstance();
cal.setTime(dateObj).
cal.get(Calendar.MONTH);
(or)
You may use String.split() also.
Use This And Pass the date Value
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
Date parse = sdf.parse("18/01/2018");
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
int date = calendar.get(Calendar.DATE);
//+1 Is Important Because if the month is January then coming 0 so Add +1
int month = calendar.get(Calendar.MONTH)+1;
int year = calendar.get(Calendar.YEAR);
System.out.println("Date:"+date +":Month:"+ month + ":Year:"+year);
In it the String is stored in an array in form of elements, and with the help of split() function, I have separated it and retrieved it from the array str[] and stored in 3 different variables day, month & year.
import java.util.*;
public class date {
public static void main(String[] args) throws Exception {
String str[] = "18/08/2012".split("/");
int day = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
int year = Integer.parseInt(str[2]);
System.out.println(day);
System.out.println(month);
System.out.println(year);
}
}

Get Last Day (Date) of Week for a given Date

How to get the last week Day (saturday) Date for a particular Date. Means if I give Input as 06-04-2012
(MM-dd-YYYY)
The output should be 06-09-2012 as seen in this calendar.
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
int leftDays= Calendar.SATURDAY - currentDay;
cal.add(Calendar.DATE, leftDays);
See
example
tl;dr
LocalDate.parse(
"06-04-2012" ,
DateTimeFormatter.ofPattern( "MM-dd-uuuu" )
).with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) )
.format( DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) )
Using java.time
The modern way is with java.time classes.
First parse your input string as a LocalDate.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" );
LocalDate ld = LocalDate.parse( "06-04-2012" , f );
ld.toString(): 2012-06-04
Then get the next Saturday, or use the date itself if it is a Saturday. To specify a Saturday, use the enum DayOfWeek.SATURDAY. To find that next or same date that is a Saturday, use an implementation of TemporaAdjuster found in the TemporalAdjusters (note plural name) class.
LocalDate nextOrSameSaturday =
ld.with( TemporalAdjusters.nextOrSame( DayOfWeek.SATURDAY ) ) ;
To generate a String, I suggest calling toString to use standard ISO 8601 format.
String output = ld.toString();
2012-06-09
But if you insist, you may use the same formatter as used in parsing.
String output = ld.format( f );
06-09-2012
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 and 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 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.
Look at either JODA time or (if you cannot add new libraries) the Calendar class.
public Calendar lastDayOfWeek(Calendar calendar){
Calendar cal = (Calendar) calendar.clone();
int day = cal.get(Calendar.DAY_OF_YEAR);
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY){
cal.set(Calendar.DAY_OF_YEAR, ++day);
}
return cal;
}

How to convert current date into string in java?

How do I convert the current date into string in Java?
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
// GET DATE & TIME IN ANY FORMAT
import java.util.Calendar;
import java.text.SimpleDateFormat;
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
Taken from here
// On the form: dow mon dd hh:mm:ss zzz yyyy
new Date().toString();
Use a DateFormat implementation; e.g. SimpleDateFormat.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String data = df.format(new Date());
tl;dr
LocalDate.now()
.toString()
2017-01-23
Better to specify the desired/expected time zone explicitly.
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
java.time
The modern way as of Java 8 and later is with the java.time framework.
Specify the time zone, as the date varies around the world at any given moment.
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ; // Or ZoneOffset.UTC or ZoneId.systemDefault()
LocalDate today = LocalDate.now( zoneId ) ;
String output = today.toString() ;
2017-01-23
By default you get a String in standard ISO 8601 format.
For other formats use the java.time.format.DateTimeFormatter class.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, 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
Later versions of Android bundle implementations of the java.time classes.
For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.
Faster :
String date = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis( ));
Most of the answers are/were valid.
The new JAVA API modification for Date handling made sure that some earlier ambiguity in java date handling is reduced.
You will get a deprecated message for similar calls.
new Date() // deprecated
The above call had the developer to assume that a new Date object will give the Date object with current timestamp. This behavior is not consistent across other Java API classes.
The new way of doing this is using the Calendar Instance.
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()
Here too the naming convention is not perfect but this is much organised.
For a person like me who has a hard time mugging up things but would never forget something if it sounds/appears logical, this is a good approach.
This is more synonymous to real life
We get a Calendar object and we look for the time in it.
( you must be wondering no body gets time from a Calendar, that is why I said it is not perfect.But that is a different topic
altogether)
Then we want the date in a simple Text format so we use a SimpleDateFormat utility class which helps us in formatting the Date from Step 1. I have used yyyy, MM ,dd as parameters in the format. Supported date format parameters
One more way to do this is using Joda time API
new DateTime().toString("yyyy-MM-dd")
or the much obvious
new DateTime(Calendar.getInstance().getTime()).toString("yyyy-MM-dd")
both will return the same result.
For time as YYYY-MM-dd
String time = new DateTime( yourData ).toString("yyyy-MM-dd");
And the Library of DateTime is:
import org.joda.time.DateTime;
public static Date getDateByString(String dateTime) {
if(dateTime==null || dateTime.isEmpty()) {
return null;
}
else{
String modified = dateTime + ".000+0000";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date dateObj = new Date();
Date dateObj1 = new Date();
try {
if (dateTime != null) {
dateObj = formatter.parse(modified);
}
} catch (ParseException e) {
e.printStackTrace();
}
return dateObj;
}
}

Java Date issues

Im having a problem with java date's, when i pass a date before 1949 into the bellow method.
The date i have returned is for example 2049, im aware it has somthing to do with the date format and thought using yyyy instead of RRRR would have fixed it. But i just dont understand why or how to reslove it. Any help will be much apreciated
public static java.sql.Date parseDate(String date) throws ParseException {
if (date != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return null;
}
Thanks Jon
let me format that for you..
public static java.sql.Date parseDate(String date) throws ParseException {
if (date != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return null;
}
This I suspect is what you want...
private Date convertDate() throws ParseException
{
String dateStr = "21-02-2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = null;
if (dateStr != null)
{
date = new Date(dateFormat.parse(dateStr).getTime());
}
System.out.println(date);
return date;
}
For 21-02-2010 you will get...
Sun Feb 21 00:00:00 GMT 2010
For 21-02-1938 you will get...
Mon Feb 21 00:00:00 GMT 1938
Does that help? I might of been due to you having MMM in your code.
tl;dr
java.sql.Date.valueOf(
LocalDate.parse( "12-Jan-23" , DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US ) )
)
Time Zone
Your code ignores the crucial issue of time zone. Determining a date requires a time zone. For any given moment, the date varies around the globe by zone.
Your code uses Date which is always in UTC. So your date value produced from the misnamed Date class will be accurate for UTC but not valid for other time zones such as America/Montreal or Asia/Kolkata.
Using java.time
The modern way to do this work is with the java.time classes that supplant the troublesome old legacy date-time classes.
The LocalDate class represents a date-only value without time-of-day and without time zone.
To parse an incoming string, define a formatting pattern with DateTimeFormatter. Specify a Locale for the human language to be used in translating the name of the month.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US );
LocalDate localDate = LocalDate.parse( "12-Jan-2017" , f );
With JDBC 4.2 and later, you can pass the LocalDate directly to your database with setObject and getObject methods.
For a JDBC driver not yet updated, fall back to the java.sql types.
java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );
LocalDate localDate = sqlDate.toLocalDate();
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 and 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 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.

Categories

Resources