首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从长格式转换为日期格式

从长格式转换为日期格式
EN

Stack Overflow用户
提问于 2012-08-02 16:04:44
回答 4查看 78.4K关注 0票数 42

我想将长值转换为字符串或日期,格式为dd/mm/YYYY。

我有一个长格式的值: 1343805819061。

可以将其转换为日期格式吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-02 16:12:11

您可以使用下面的代码行来完成此操作。这里的timeInMilliSecond是长值。

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

或者你也可以使用下面的代码。

 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();

参考:- DateFormatSimpleDateFormat

附注:根据您的需要更改日期格式。

票数 96
EN

Stack Overflow用户

发布于 2012-08-02 16:13:53

您可以在Date实例或contructor (Long)上使用setTime方法;

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

然后使用简单的日期格式

请参阅http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

票数 5
EN

Stack Overflow用户

发布于 2020-03-13 20:57:24

如果长度是10位,那么试试这个

long DateInLong= 1584212400;

Date date =新日期(DateInLong*1000L);

SimpleDateFormat simpledateformate=新SimpleDateFormat("yyyy-MM-dd");

String DATE =simpledateformate.format(日期);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11773369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档