Java: easiest way to subtract dates

I created a class with two fields that should be dates, start_dateand date_passed. I learned the best way in java to have dates in a format YYYY MM DDthat makes it easy to subtract the date and the ability to "adjust" the date, for example, in the future, for example.

An example of what I would like to do ...

library.circulate_book("Chemistry", **start date here**) //actual date or random date
library.pass_book("Chemistry", **Date Passed here**) //random date such as 5 days after start date
int days_had = Date_Passed - start_date

So far I have found many ways to format dates using calendars and Date classes, but have not yet found one that looks like it will work, given that most dates end in strings. Any suggestions / small examples are welcome! Also, any links to examples would be great!

+4
source share
5 answers

Syntactic

You must first parse your string inputs into date objects. Then you work on preparing business logic with these objects.

Stop thinking of date and time values ​​as strings that will alert you. We work with date and time objects in our code; we exchange data with users or other applications using the String representation of this time object.

In Java 8 and later, use java.time . See Tutorial .

You only need a date without a time of day, so we can use the class LocalDate.

- , , .

String input = "2015 01 02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy MM dd" );
LocalDate localDate = formatter.parse ( input , LocalDate :: from );

StackOveflow.com. , X Java-?. . , . : "" - .

, Period, , .

LocalDate weekLater = localDate.plusDays ( 7 );
Period period = Period.between ( localDate , weekLater );
Integer daysElapsed = period.getDays ();

.

System.out.println ( "localDate: " + localDate + " to " + weekLater + " in days: " + daysElapsed );

localDate: 2015-01-02 2015-01-09 : 7

, Period , . , , . (ZonedDateTime ), ThreeTen-Extra projects Days , / .

+3

Java-8 - , :

String startDate = "2016 01 02";
String passedDate = "2016 02 29";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDate date1 = LocalDate.parse(startDate, formatter);
LocalDate date2 = LocalDate.parse(passedDate, formatter);

long elapsedDays = ChronoUnit.DAYS.between(date1, date2);
System.out.println(elapsedDays); // 58 (correct)

java.time.Period.getDays() , . Period P1M27D, ( ):

System.out.println(Period.between(date1, date2).getDays()); // 27 (WRONG!!!)

, java.util.Date, GregorianCalendar ... Tacktheritrix, , - java.util.Date, - ( ).

. 8 . , , Java-8. , , , Java-8 - . Java-8 Basil Bourque, , Java-8, , .

+3

Here's the answer using Calendar:

Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

cal2.setTime(cal.getTime());
cal2.add(Calendar.DAY_OF_YEAR, 5);
System.out.println((cal2.getTimeInMillis() - cal.getTimeInMillis()) / (1000d * 60 * 60 * 24));
+1
source

If you are stuck in old java you can use SimpleDateFormat.

    //For substraction
    long differenceInMillis = date1.getTime() - date2.getTime();

    //Date Format
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd");
    String dateAsString = dateFormat.format(date1); //To get a text representation
    Date dateParsed = dateFormat.parse(dateAsString); //To get it back as date
0
source

Source: https://habr.com/ru/post/1614502/


All Articles