Java Time and Date

This tutorial will show how to create a time stamp with Java. Since Java does not have a built in package for handing dates and time, the package java.time will need to be imported. This package is an API for dates, times, instants, and durations. Here is a link to the java.time documentation if you would like more detail.

Here are five of the most useful classes that come with java.time :

ClassDescription
LocalDate Current date(year,month,day)
LocalTime Current time (8:30 am)
LocalDateTime Current date & time ()
Instance Creates a Time Stamp
ZoneDateTime Current date & time with time-zone
import java.time.LocalDate;// importing LocalDate class
import java.time.Instant;//importing Instant class

public class Example{
  public static void main(String[] args) {
	  
    LocalDate current = LocalDate.now(); 
    Instant timeStamp = Instant.now(); 
    
    System.out.println(current); // Print out current date 
    System.out.println(timeStamp); // Print out Time Stamp 
  }
}

Output

Current Date : 2020-12-03
Time Stamp : 2020-12-03T21:06:58.325913800Z

java.time has other classes that allow dates to be moved durations and periods. Durations are movements on a time line in nanoseconds and Periods are movements like days, years or months.

The package comes with the built in keyword .plus() . This can be used to move incrementally forward in a timeframe .

.plusYears() Move a given # of Years
.plusDays()Move a given # of Days
.plusWeeks()Move a given # of Weeks
.plusMoths()Move a given # of Moths

java.time also comes with .minus() . This can be used to move incrementally backwards in a timeframe.

.minusDays()Subtract a given # of Days
.minusWeeks()Subtract a given # of Weeks
.minusMoths()Subtract a given # of Moths
.minusYears()Subtract a given # of Years
import java.time.LocalDate;// importing LocalDate class

public class Example{
  public static void main(String[] args) {
	  
	  LocalDate future = LocalDate.now().plusDays(300);//getting current date and add 300 days
	  
	  LocalDate past = LocalDate.now().minusDays(100);// minus 100 days from today 
	  
	  LocalDate next = LocalDate.now().plusMonths(1);// Next month from today
	  
	  
	  System.out.println(future);
	  System.out.println(past);
	  System.out.println(next);
	  

  }
}

Output :

2021-10-02
2020-08-28
2021-01-06

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the…

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value…

Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s