Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from Long to date format

I want to convert Long value to String or Date in this format dd/mm/YYYY.

I have this value in Long format: 1343805819061.

It is possible to convert it to Date format?

like image 520
HaOx Avatar asked Aug 02 '12 08:08

HaOx


People also ask

How do you convert a long number to a date?

Select the cells that have the number that you want to convert into a date. Click the 'Home' tab. In the 'Number' group, click on the Number Formatting drop-down. In the drop-down, select 'Long Date' or Short Date' option (based on what format would you want these numbers to be in)

What is date in long format?

For example, you can combine the General Date and Long Time formats as follows: m/dd/yyyy h:mm:ss.


2 Answers

You can use below line of code to do this. Here timeInMilliSecond is long value.

 String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(TimeinMilliSeccond)); 

Or you can use below code too also.

 String longV = "1343805819061";  long millisecond = Long.parseLong(longV);  // or you already have long value of date, use this instead of milliseconds variable.  String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString(); 

Reference:- DateFormat and SimpleDateFormat

P.S. Change date format according to your need.

like image 82
AAnkit Avatar answered Oct 09 '22 10:10

AAnkit


You can use method setTime on the Date instance or the contructor Date(long);

setTime(long time)        Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.  Date(long date)        Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT 

Then use the simple date formater

see http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

like image 40
Mark Bakker Avatar answered Oct 09 '22 10:10

Mark Bakker