Java Date calculation example
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import com.javadevjournal.Java.DateDifferenceExample;
|
||||
import com.javadevjournal.java8.Java8DateCalculation;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args ) throws ParseException {
|
||||
Java8DateCalculation dateDifferenceExample = new Java8DateCalculation();
|
||||
dateDifferenceExample.calculate_difference_between_dates();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.javadevjournal.Java;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DateDifferenceExample {
|
||||
|
||||
public void calculate_difference_between_dates() throws ParseException {
|
||||
String currentDate= "10/24/2017";
|
||||
String finalDate= "10/28/2017";
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
|
||||
Date firstDate = simpleDateFormat.parse("06/24/2017");
|
||||
Date secondDate = simpleDateFormat.parse("06/30/2017");
|
||||
|
||||
long difference = Math.abs(firstDate.getTime() - secondDate.getTime());
|
||||
long differenceDates = difference / (24 * 60 * 60 * 1000);
|
||||
|
||||
//Convert long to String
|
||||
String dayDifference = Long.toString(differenceDates);
|
||||
System.out.println("Day Differnec is " + dayDifference);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.javadevjournal.java8;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class Java8DateCalculation {
|
||||
|
||||
public void calculate_difference_between_dates(){
|
||||
|
||||
//Use LocalDate and ChronoUnit
|
||||
LocalDate firstDate = LocalDate.of(2017, 5, 6);
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
|
||||
long days = ChronoUnit.DAYS.between(firstDate, currentDate);
|
||||
System.out.println(days);
|
||||
|
||||
//date calculation
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate tenDaysAhead = now.plusDays(10);
|
||||
System.out.println(tenDaysAhead.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user