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

Python019

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

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

得到一个毫秒时间,是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))

}

}

import java.text.ParseException

import java.text.SimpleDateFormat

public class Cat {

public static void main(String[] args) throws ParseException {

String str = "201104141302"

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm")

long millionSeconds = sdf.parse(str).getTime()//毫秒

System.out.println(millionSeconds)

}

}

具体操作方式:

将时间字符串转化为 long字符串

将long字符串转化为时间格式化输出

将系统当前时间转换为字符串

TimeSpan ts = new TimeSpan(1251790200000*10)

ts.Days表示对应的天数

ts.Hours表示对应的小时数,

依次推类.

注意:

// Parameters:

// ticks:

// A time period expressed in 100-nanosecond units.

public TimeSpan(long ticks)

你还可以用DateTime类型去Add这个类型的变量,得到加减后的时间.