首先,你需要有一个表示日期和时间的long类型值,这通常是通过System.currentTimeMillis()方法获取的当前时间戳,或者是一个已经存在的表示过去或未来时间的long值。 使用Java的Date类或LocalDateTime类将long值转换为日期对象: 对于Java 8及之前的版本,你可以使用java.util.Date类;对于Java 8及之后的版本,推荐使用java....
importjava.util.Date;importjava.text.SimpleDateFormat;publicclassLongToDateString{publicstaticvoidmain(String[]args){// 获取当前时间戳longcurrentTimestamp=System.currentTimeMillis();// 将 long 转换为 DateDatedate=newDate(currentTimestamp);// 设置日期格式SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM...
//long转Date private Date LongToDate(long str) { Date date = new Date(str*1000); return date; } //long转String private String LongToString(long str) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String string = sdf.format(str*1000); return string; } 发布者:全栈程序...
方法一:使用 SimpleDateFormat SimpleDateFormat是 Java 早期提供的时间格式化工具。我们可以使用它来将 Long 类型的时间戳转换为 String 类型。 importjava.text.SimpleDateFormat;importjava.util.Date;publicclassTimestampToString{publicstaticvoidmain(String[]args){// 获取当前时间戳longtimestamp=System.currentTime...
Java中Long、String、Date 类型之间的转换 1、Java.util.Date类型转换成long类型 Date date=new Date(); System.out.println(date.getTime()); 解析:其中getTime()返回为long类型,长度为13,表示毫秒;如果想获得秒数,只需要除以1000即可。 long mseconds=date.getTime()/1000; ...
SimpleDateFormat(dateFormat);6 Date date = new Date(millSec);7return sdf.format(date);8 } 上⾯是⽅法第⼀个参数:dateFormat 指的是你希望转换成的String类型的样式第⼆个参数:需要转换的时间 long类型例如:transferLongToDate("yyyy-MM-dd HH:mm:ss",1245678944);
String转date Date result = new DateTime(param).toDate(); String转Long Long result = Long.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(param).getTime() / 1000); Date转String String result = new SimpleDateFormat("yyyyMMddHHmmss").format(param); ...
1/**2* 把long 转换成 日期 再转换成String类型3*/4publicString transferLongToDate(String dateFormat, Long millSec) {5SimpleDateFormat sdf =newSimpleDateFormat(dateFormat);6Date date =newDate(millSec);7returnsdf.format(date);8} 上面是方法 第一个参数:dateFormat 指的是你希望转换成的String类...
* @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss) * @param millSec(毫秒数) * @return */ private static String transferLongToDate(String dateFormat,Long millSec){ SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); Date date= new Date(millSec); return sdf.format(date); ...
方法一:使用SimpleDateFormat类 Java中的SimpleDateFormat类提供了将Date对象格式化为指定的字符串的功能。我们可以通过将long类型的时间戳转换为Date对象,然后使用SimpleDateFormat类将其格式化为字符串。 下面是一个使用SimpleDateFormat类的示例代码: importjava.text.SimpleDateFormat;importjava.util.Date;publicclassLon...