java把long转换成date的日期格式有两种方法,使用date的构造函数和calendar的settime方法,如下:
1.date类的构造函数直接传入long型的数据:
long time = system.currenttimemillis();
date date = new date(time);2.使用calendar类的settime方法,传入long型的数据:
calendar ca = calendar.getinstance();
long time = system.currenttimemillis();
ca.settime(time );
date d = ca.gettime();
② 如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据
Date类型并没有格式,只有转换成String格式的时候让格式化显示。
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());
Calendar calendar = Calendar.getInstance();
int year = Integer.parseInt(datetime.substring(0,4));
int month = Integer.parseInt(datetime.substring(5,7));
int date = Integer.parseInt(datetime.substring(8,10));
int hour = Integer.parseInt(datetime.substring(11,13));
int minute = Integer.parseInt(datetime.substring(14,16));
//int second = Integer.parseInt(datetime.substring(17,19));
if(calendar.get(Calendar.YEAR)>year){
int y = calendar.get(Calendar.YEAR)-year;
(2)javadate转换long扩展阅读:
Date类可以在java.util包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。
import java.util.*;
public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = now.getTime();
System.out.println("Value is " + nowLong);
③ 如何用Java把date类型转换成long数字
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)
{
Datedate=newDate();
//返回自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数。
longtime=date.getTime();
System.out.println(time);
}