1

I am not very familiar with date objects so I thought I'd ask how would I ask a user to input the a date that is compatible with the following method I found to find the difference in days:

 public long diferenciaFecha(LocalDate pfecha1, LocalDate pfecha2){
  LocalDate fecha1 = pfecha1;
  LocalDate fecha2 = pfecha2;

  Period p = Period.between(fecha1, fecha2);
  long resul = ChronoUnit.DAYS.between(fecha1, fecha2);

  System.out.println(" Han pasado: " + resul + " dias.)");

  return resul;

      }   
 }

What would the input format look like?

Thanks a lot in advance

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Code Grasshopper
  • 610
  • 1
  • 11
  • 29

2 Answers2

1

Time related programming can be very frustrating. Temporal Classes were invented to help manipulate time values and to help convert text and numeric values into time values. If you are using Java classes like LocalDate and ChronoUnit, you can look them up on Oracle's site with a simple google search of the class name.

If you are have trouble with days, I strongly suggest you give temporal classes a review because when you get to manipulating hours and time-zones,etc. it will be more complicated.

http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

"public final class LocalDate

extends Object

implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03. "

http://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html

DataBee
  • 26
  • 2
1

Your code seems to be using the new java.time package in Java 8. To be sure, look at the import statements.

The LocalDate class uses static factory methods to create instances rather than constructors. See the doc for several methods.

LocalDate now = LocalDate.now ( ZoneId.of( "America/Montreal" ) );

Or…

LocalDate localDate = LocalDate.of( 2014, 1, 23 );  // year, month, day.

If the user is entering text, parse as numbers.

LocalDate localDate = LocalDate.of( Integer.parseInt( "2014") , Integer.parseInt( "1") , Integer.parseInt( "23") );

Or parse as one string in standard ISO 8601 format.

LocalDate localDate = LocalDate.parse( "2014-01-23" ) ;

For other formats, search Stack Overflow for many discussions and examples of parsing strings with 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.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154