Java program to convert Long to Date Object and Date Object to Long

How to convert Long to Date Object and Date Object to Long:

The following Java program is used to convert date from long to Date object and Date object to long.

Long to Date Object,

Syntax: Long l=1346524199000l;

Date d=new Date(l);

Get the Date in long datatype and convert to Date object using the above code.

Date object to long,

Syntax: Long l1=d1.getTime();

Create a Date object and get current date using Date(). Convert the obtained date to long using getTime() method.

longformat.java

package com.candidjava.time;

import java.text.ParseException;
import java.util.Date;

public class LongFormat 
{
public static void main(String[] args) throws ParseException
{
	//long to date object
	Long l=1346524199000l;
	Date d=new Date(l);
	System.out.println(d);
	
	//date to long
	Date d1=new Date();
	Long l1=d1.getTime();
	System.out.println(l1);
}
}

Output:

Sat Sep 01 23:59:59 IST 2012
1546848563069

Leave a Reply

Your email address will not be published. Required fields are marked *