将java怎么将long类型的时间转换成年月日的形式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将java怎么将long类型的时间转换成年月日的形式相关的知识,希望对你有一定的参考价值。

用java代码实现:

public static String longToDate(long lo)    

Date date = new Date(lo);   

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    

return sd.format(date);
 

资料拓展:

长整型(long)是计算机程序语言,是程序设计中数据类型的一种表现方式,一般情况下用long 表示长整型。 long 有符号64位整数,范围是-2^63-2^63 -1 Int64。

参考技术A 多看点JDK API。
常用的时间转换形式:long <==>Date <==>String
long =>Date :new Date(longValue);
Date =>String:new SimpleDateFormat("yyyy-MM-dd").format(date);
参考技术B

    其实就是将毫秒数转换为日期

    具体方法

        /**

         * 把毫秒转化成日期

         * @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)

         * @param millSec(毫秒数)

         * @return

         */

        pubic static  String transferLongToDate(String dateFormat,Long millSec)

         SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

        Date date= new Date(millSec);

                return sdf.format(date);

       

参考技术C import java.util.Calendar;
import java.util.Formatter;
import java.util.Locale;

public class Bdzd

/**
* @param args
*/
public static void main(String[] args)
String datestr = getDateStr(System.currentTimeMillis());
System.out.println(datestr);


private static String getDateStr(long millis)
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
Formatter ft=new Formatter(Locale.CHINA);
return ft.format("%1$tY年%1$tm月%1$td日%1$tA,%1$tT %1$tp", cal).toString();

参考技术D SimpleDateFormat 楼上有回答的

js怎么把时间转换成年月日时分秒的形式

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:var a = 1562060142000;document.body.innerText = new Date(a);。

3、浏览器运行index.html页面,此时时间被转化为年月日时分秒打印出来了。

参考技术A function dateFtt(fmt,date)
//author: meizz
var o =
"M+" : date.getMonth()+1, //月份
"d+" : date.getDate(), //日
"h+" : date.getHours(), //小时
"m+" : date.getMinutes(), //分
"s+" : date.getSeconds(), //秒
"q+" : Math.floor((date.getMonth()+3)/3), //季度
"S" : date.getMilliseconds() //毫秒 ;
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;

//创建时间格式化显示function crtTimeFtt(value,row,index)
var crtTime = new Date(value); return top.dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);//直接调用公共JS里面的时间类处理的办法
参考技术B 如果你红框内返回的是毫秒数,你就要先转换为date类型 比如红框内是X ;
var now = new Date(X);
var year = now.getFullYear(); //获取年份
var month = now.getMonth(); //获取月份 月份要+1
var date = now.getDate(); //获取日期
var hour = now.getHours(); //获取时
var minu = now.getMinutes(); //获取分钟
var sec = now.getSeconds(); //获取秒钟
这些都是原生的js方法追问

红框里返回给前端的是创建时间距离现在时间的天数,createTime是数据库里的字段

参考技术C var time1 = new Date().format("yyyy-MM-dd hh:mm:ss");这种就行,你试试追问

直接在后面加吗

追答

是的

以上是关于将java怎么将long类型的时间转换成年月日的形式的主要内容,如果未能解决你的问题,请参考以下文章

Java经典入门教程!日期转换成年月日格式java

如何将时间转化成年月日时分秒 js

js中怎么吧long型的日期转换成String类型的

sql server 数据库,在查询sql语句中日期格式转换问题,怎么把原数据年月日时分秒转换成年月日

java 我获得单位为毫秒的当前时间,如何转化成年月日小时分格式?

date.gettime()返回的整数值怎么转换成年月日时分秒?