Java @ Desk: Core Java 8 | Java at your desk

LocalDateTime Add Hours to Date in Java 8

LocalDateTime Add Hours to Date in Java 8

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to add days to date:

1) plusHours - Takes an integer. Date will be incremented by number of hours.

2) plus - Takes an argument of class Duration.
The integer passed should be in number of hours.

Both these methods returns copy of LocalDateTime object.

package com.datentime;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class DateFormatter_Add {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss");
 
 public static void addHoursToDateUsingPlusHours() {
  LocalDateTime sysTime = LocalDateTime.now();
  LocalDateTime one = sysTime.plusHours(1);
  System.out.println("System Time : \t" + sysTime.format(formatter));
  System.out.println("Time after one hour will be : \t"
    + one.format(formatter));
 }

 public static void addHoursToDateUsingPlusDuration() {
  LocalDateTime sysTime = LocalDateTime.now();
  System.out.println("System Time : \t" + sysTime.format(formatter));
  LocalDateTime one = sysTime.plus(Duration.ofHours(1));
  System.out.println("Time after one hour will be (using Duration): \t"
    + one.format(formatter));
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  addHoursToDateUsingPlusHours();
  addHoursToDateUsingPlusDuration();
 }
}


Output:

System Time : 21-Aug-2014 12:26:35
Time after one hour will be : 21-Aug-2014 13:26:35
System Time : 21-Aug-2014 12:26:35
Time after one hour will be (using Duration): 21-Aug-2014 13:26:35


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.

LocalDateTime Add Days to Date in Java 8

LocalDateTime Add Days to Date in Java 8

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to add days to date:

1) plusDays - Takes an integer. Date will be incremented by number of days.

2) plus - Takes an argument of class Duration.
The integer passed should be in number of days.

Both these methods returns copy of LocalDateTime object.

package com.datentime;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class DateFormatter_Add {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss"); 
 
 public static void addDaysToDateUsingPlusDays() {
  LocalDateTime today = LocalDateTime.now();
  LocalDateTime tomorrow = today.plusDays(1);
  System.out.println("Today is : \t" + today.format(formatter));
  System.out
    .println("Tommorow will be : \t" + tomorrow.format(formatter));
 }

 public static void addDaysToDateUsingPlusDuration() {
  LocalDateTime today = LocalDateTime.now();
  LocalDateTime tomorrow = today.plus(Duration.ofDays(1));
  System.out.println("Today is : \t" + today.format(formatter));
  System.out
    .println("Tommorow will be (Using Duration) : \t" + tomorrow.format(formatter));
 } 

 public static void main(String[] args) {
  // TODO Auto-generated method stub  
  addDaysToDateUsingPlusDays();
  addDaysToDateUsingPlusDuration();
 }
}


Output :

Today is : 21-Aug-2014 12:26:35
Tommorow will be : 22-Aug-2014 12:26:35
Today is : 21-Aug-2014 12:26:35
Tommorow will be (Using Duration) : 22-Aug-2014 12:26:35


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.

LocalDateTime Add Weeks to Date in Java 8

LocalDateTime Add Weeks to Date in Java 8

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to subtract the hours from the date:

1) plusWeeks - Takes an integer. Date will be incremented by number of weeks.

2) plus - Takes an argument of class Duration.
The integer passed should be in number of days.

Both these methods returns copy of LocalDateTime object.

package com.datentime;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class DateFormatter_Add {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss");

 
 public static void addWeeksToDateUsingPlusWeeks() {

  LocalDateTime xmas  = LocalDateTime.of(2013, Month.DECEMBER, 25, 0, 0);
  LocalDateTime newYear = xmas.plusWeeks(1);
  System.out.println("Christmas bells will ring from : \t" + xmas.format(formatter));
  System.out.println("New year will be on  : \t"
    + newYear.format(formatter));

 }

 public static void addWeeksToDateUsingPlusDuration() {
  
  LocalDateTime xmas  = LocalDateTime.of(2013, Month.DECEMBER, 25, 0, 0);
  LocalDateTime newYear = xmas.plus(Duration.ofDays(7));
  System.out.println("Christmas bells will ring from: \t" + xmas.format(formatter));
  System.out.println("New year will be on  : \t"
    + newYear.format(formatter));
 }
 
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  addWeeksToDateUsingPlusWeeks();
 addWeeksToDateUsingPlusDuration();
 
 }
}


Output :

Christmas bells will ring from : 25-Dec-2013 00:00:00
New year will be on  :  01-Jan-2014 00:00:00
Christmas bells will ring  from : 25-Dec-2013 00:00:00
New year will be on  :  01-Jan-2014 00:00:00


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.

How to subtract seconds from Date using java 8 api

How to subtract seconds from Date using java 8 api

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to subtract the hours from the date:

1) minusSeconds - Takes an integer. Date will be reduced by given number of seconds

2) minus - Takes an argument of class Duration

Both these methods returns copy of LocalDateTime object.


In the below example, Date is initialized to 1st Jan 2014.
10 seconds are deducted using both the methods.


import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class SubtractSecondsFromDate {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss");

 
 
 public static void subSecondsFromDate_withMinusSeconds() {
        // Date is initialized to 1st Jan 2014
 LocalDateTime newYear = LocalDateTime.parse("2014-01-01T00:00:00");
  LocalDateTime newYearcountDown = newYear.minusSeconds(10);
 System.out.println("Lets the count down begin    : \t"
    + newYearcountDown.format(formatter));
 System.out.println("Happy New Year   : \t" + newYear.format(formatter));
  
 }

 public static void subSecondsFromDate_usingMinusDurationofSeconds() {

      // Date is initialized to 1st Jan 2014
  LocalDateTime newYear = LocalDateTime.parse("2014-01-01T00:00:00");
   LocalDateTime newYearcountDown = newYear.minus(Duration.ofSeconds(10));
  System.out.println("Lets the count down begin    : \t"
     + newYearcountDown.format(formatter));
  System.out.println("Happy New Year  (using Duration) : \t" + newYear.format(formatter));
 }

 public static void main(String[] args) {
  
  subSecondsFromDate_withMinusSeconds();
  subSecondsFromDate_usingMinusDurationofSeconds();

 }
}


Output :

Lets the count down begin : 31-Dec-2013 23:59:50
Happy New Year : 01-Jan-2014 00:00:00
Lets the count down begin : 31-Dec-2013 23:59:50
Happy New Year  (using Duration) :  01-Jan-2014 00:00:00


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.

How to subtract minutes from Date using java 8 api

How to subtract minutes from Date using java 8 api

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to subtract the hours from the date:

1) minusMinutes - Takes an integer. Date will be reduced by given number of minutes
2) minus - Takes an argument of class Duration

Both these methods returns copy of LocalDateTime object.

This time we will use the (to) factory of LocalDateTime and pass the custom parameters. Let’s say your cake should be ready by half an hour. All the core classes in the new API are constructed by fluent factory methods. When constructing a value by its constituent fields, the factory is called of;

In the below example, Date is initialized to 5th DECEMBER 2014.
30 minutes are deducted using both the methods.


import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class SubtractMinutesFromDate {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss");

 
 public static void subMinutesFromDate_withMinusMinutes() {
        // Date is initialized to 5th DECEMBER 2014
  LocalDateTime birthday = LocalDateTime.of(2014, Month.DECEMBER, 5, 0, 0); 
  LocalDateTime birthdayParty = birthday.minusMinutes(30);
  System.out.println("Birthday Bash   : \t" + birthday.format(formatter));
  System.out.println("But cake should be ready by    : \t"
    + birthdayParty.format(formatter));

 }

 public static void subMinutesFromDate_usingMinusDurationofMins() {

      // Date is initialized to 5th Dec 2014
   LocalDateTime birthday = LocalDateTime.of(2014, Month.DECEMBER, 5, 0, 0); 
   LocalDateTime birthdayParty = birthday.minus(Duration.ofMinutes(30));
   System.out.println("Birthday Bash   : \t" + birthday.format(formatter));
   System.out.println("But cake should be ready by   (using Duration) : \t"
     + birthdayParty.format(formatter));
 }
 
 

 public static void main(String[] args) {
  subMinutesFromDate_withMinusMinutes();
  subMinutesFromDate_usingMinusDurationofMins();
 }
}


Output :

Birthday Bash : 05-Dec-2014 00:00:00
But cake should be ready by : 04-Dec-2014 23:30:00
Birthday Bash : 05-Dec-2014 00:00:00
But cake should be ready by   (using Duration) : 04-Dec-2014 23:30:00


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.

How to subtract hours from Date using java 8 api

How to subtract hours from Date using java 8 api

In Java 8, java.time.LocalDateTime class is used to perform manipulation on the date value. Class itself has 2 methods to subtract the hours from the date:

1) minusHours
2) minus

Both these methods returns copy of LocalDateTime object.


We are using LocalDateTime.now(); to get current system time.

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;

public class SubtractHoursFromDate {
 static java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
   .ofPattern("dd-MMM-yyyy HH:mm:ss");

 public static void subHoursFromDate_withMinusHours() {

  LocalDateTime sysTime = LocalDateTime.now();
  LocalDateTime one = sysTime.minusHours(1);
  System.out.println("System Time : \t" + sysTime.format(formatter));
  System.out.println("System Time Minus one hour: \t" + one.format(formatter));

 }

 public static void subHoursFromDate_usingMinusDuration() {

  LocalDateTime sysTime = LocalDateTime.now();
  System.out.println("System Time : \t" + sysTime.format(formatter));
  LocalDateTime one = sysTime.minus(Duration.ofHours(1));
  System.out.println("System Time Minus one hour (using Duration): \t" + one.format(formatter));
 }

 

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  subHoursFromDate_withMinusHours();
  subHoursFromDate_usingMinusDuration();
  

 }


Output:

System Time :  20-Aug-2014 20:53:37
System Time Minus one hour:  20-Aug-2014 19:53:37
System Time :  20-Aug-2014 20:53:37
System Time Minus one hour (using Duration):  20-Aug-2014 19:53:37


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.