Obtaining a Machine Timestamp - Java Date Time

Java examples for Date Time:Instant

Introduction

Instant class represents the start of a nanosecond on the timeline based on machine time.

Demo Code

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args){
            Instant timestamp = Instant.now();
            System.out.println("The current timestamp: " + timestamp);

            //Now minus three days
            Instant minusThree = timestamp.minus(3, ChronoUnit.DAYS);
            System.out.println("Now minus three days:" + minusThree);

            ZonedDateTime atZone = timestamp.atZone(ZoneId.of("GMT"));
            System.out.println(atZone);

            Instant yesterday = Instant.now().minus(24, ChronoUnit.HOURS);
            System.out.println("Yesterday: " + yesterday);
    }/* www. j  a v a  2s  .c  o m*/
    
}

Result


Related Tutorials