Java – How to get current epoch timestamp | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Get current timestamp in Java using System.currentTimeMillis()

package com.example.java.programming.datetime;

/**
 * @author code.factory
 *
 */
public class Test {
	public static void main(String... args) {
		// Get epoch timestamp using System.currentTimeMillis()
		long currentTimestamp = System.currentTimeMillis();
		System.out.println("Current epoch timestamp in millis: " + currentTimestamp);
	}
}

Output :

Current epoch timestamp in millis: 1593423347845

Get current timestamp in Java using the Instant class

package com.example.java.programming.datetime;

import java.time.Instant;

/**
 * @author code.factory
 *
 */
public class Test {
	public static void main(String... args) {
		// Get current timestamp using Instant
		long currentTimestamp = Instant.now().toEpochMilli();
		System.out.println("Current epoch timestamp in millis: " + currentTimestamp);
	}
}

Output :

Current epoch timestamp in millis: 1593423516547

Leave a comment