问题描述:
得到一个毫秒时间,是1970年1月1日0:00至今的毫秒时间,怎么转换成通常格式?
解析:
import java.util.*
import java.text.SimpleDateFormat
public class test
{
public static void main (String args[])
{
Date d = new Date()
long longtime = d.getTime()
System.out.println(longtime)
你获得的是上面的long型数据吧
String time = d.toLocaleString()
你可以简单的得到本地化时间,本来就是String类型的就不用转换了
System.out.println(time)
也可以自己用SimpleDateFormat这个函数把它变成自己想要的格式,注意需要import java.text.SimpleDateFormat
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
System.out.println(sdf.format(longtime))
}
}
时间格式化输出主要有两种方式,代码如下:
//使用Calendar
Calendar now = Calendar.getInstance();
System.out.println("年:" + now.get(Calendar.YEAR));
System.out.println("月:" + (now.get(Calendar.MONTH) + 1));
System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分:" + now.get(Calendar.MINUTE));
ystem.out.println("秒:" + now.get(Calendar.SECOND));
//使用Date
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间:" + sdf.format(d));
扩展资料
JAVA中获取当前系统时间。
import java.util.Date
import java.text.SimpleDateFormat
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//设置日期格式
System.out.println(df.format(new Date()))// new Date()为获取当前系统时间
}
}
参考资料来源:百度百科:Java