Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

wazowski ILapitan

🎯
Focusing
View GitHub Profile
@ILapitan
ILapitan / dates1.7.java
Created Jun 4, 2017
Calculate the number of days between two dates in Java 1.7
View dates1.7.java
//input dates
final String start = "2010-01-15";
final String end = "2011-03-18";
// parse the dates
final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
final Date startDate = dateFormat.parse( start );
final Date endDate = dateFormat.parse( end );
// calculate the number of days between start and end dates
long daysDuration = Math.round( ( endDate.getTime() - startDate.getTime() ) / (double) ( 1000 * 60 * 60 * 24 ) );
@ILapitan
ILapitan / dates_with_time1.8.java
Last active Jun 4, 2017
Calculate the number of days between two dates in Java 1.8 (with time)
View dates_with_time1.8.java
// input dates
final String start = "2010-01-15T00:00:00";
final String end = "2011-03-18T18:00:00";
// parse the dates
final LocalDateTime startDate = LocalDateTime.parse(start);
final LocalDateTime endDate = LocalDateTime.parse(end);
// calculate the number of days between start and end dates
long daysDuration = Duration.between(startDate, endDate).toDays();
@ILapitan
ILapitan / dates1.8.java
Last active Jun 4, 2017
Calculate the number of days between two dates in Java 1.8
View dates1.8.java
// input dates
final String start = "2010-01-15";
final String end = "2011-03-18";
// parse the dates
final LocalDate startDate = LocalDate.parse( start );
final LocalDate endDate = LocalDate.parse( end );
// calculate the number of days between start and end dates
long daysDuration = ChronoUnit.DAYS.between( startDate , endDate );