How to get Current Timestamp in Java

In this article, you will see how to get current timestamp in Java

Using Date Class

package com.kruders.core;

import java.sql.Timestamp;
import java.util.Date;

public class GetCurrentTimeStamp {
	public static void main( String[] args ) {
		Date date= new Date();
		System.out.println(new Timestamp(date.getTime()));
    }
}

Using Calendar Class

package com.kruders.core;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

public class GetCurrentTimeStamp {
	public static void main( String[] args ) {
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		System.out.println(new Timestamp(now.getTime()));
    }
}

When you run the above example you’ll get an output like:

2012-11-22 10:04:18.843


No comments yet.

Leave a Reply