Convert from Long to date format

断了今生、忘了曾经 提交于 2019-11-30 03:01:00
AAnkit

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.

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

java.util.Date dateObj = new java.util.Date(timeStamp);

Here timeStamp is your long integer which is actually timestamp in millieseconds, you get the java date object, now you can convert it into string by this

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy");

StringBuilder nowYYYYMMDD = new StringBuilder( dateformatYYYYMMDD.format( dateObj ) );
StringBuilder nowMMDDYYYY = new StringBuilder( dateformatMMDDYYYY.format( dateObj ) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!