Java Date calculation example

This commit is contained in:
Umesh Awasthi
2018-04-29 09:28:02 -07:00
parent b7c974db9f
commit 2779473cc2
5 changed files with 161 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevjournal</groupId>
<artifactId>java-date-calculation</artifactId>
<version>1.0-SNAPSHOT</version>
<name>java-date-calculation</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -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 );
}
}