导航:首页 > 编程语言 > java时间转换毫秒

java时间转换毫秒

发布时间:2024-05-23 19:11:13

java date类型的数据怎么能显示到毫秒

使用java.sql包下的Timestamp(参数) 参数是时间戳 就可以显示到毫秒

❷ JAVA中如何获取毫秒和微秒数

一、获取毫秒数的代码:

微秒使用System.nanoTime()方法:如果Java程序需要高精度的计时,如1毫秒或者更小,使用System.nanoTime()方法,可以满足需求。

(2)java时间转换毫秒扩展阅读:

获取微秒函数System.nanoTime() 的隐患:

System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的,而System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的。

但是在多核处理器上,由于每个核心的开始时间不确定,那么

“long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start;”

这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑。

❸ java将时间段转成分钟数

实现思路:将两个时间转换为Timestamp类型(单位为毫秒),所以只需要计算出两个数值的差,之后直接将毫秒单位转换为秒,之后在转化为分钟就可以了:
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = sdf.parse("2015-10-22 05:12:10");
Date end = sdf.parse("2013-10-23 08:10:10");
(end.getTime() - start.getTime())/(1000*60);
知识普及:时间计算都是从1970年1月1日开始计算的。

❹ java怎么把时间转化为毫秒值

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);
}
}
输出结果就是:1302757320000

❺ java中怎样将时间年月日转换成时间格式再转换成毫秒

publicclassTestTime{
publicstaticvoidmain(String[]args){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy年M月d日");
try{
Dated=sdf.parse("2013年1月6日");
sdf=newSimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(d));
System.out.println(d.getTime());
}catch(ParseExceptione){
e.printStackTrace();
}
}
}

❻ java 毫秒转换时间

时间除以1000转换成秒,对60取余就是秒数,除以60后再对60取余是分,除以60后再对24取余是小时

❼ java里如何转换"Wed Apr 11 16:18:42 +0800 2012"这样的日期格式,我希望把它转成long型的毫秒数

package Serial2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateTest {

public static final String SOURCE = "Wed Apr 11 16:18:42 +0800 2012";

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

SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss Z yyyy", new Locale("ENGLISH", "CHINA"));

Date myDate = sdf.parse(SOURCE);
System.out.println(myDate);

sdf.applyPattern("EEE MMM dd HH:mm:ss Z yyyy");
System.out.println(sdf.format(myDate));

SimpleDateFormat sdf2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", new Locale("CHINESE", "CHINA"));
System.out.println(sdf2.format(myDate));

sdf2.applyPattern("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf2.format(myDate));

long miliSeconds = myDate.getTime();
System.out.println("自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:"+miliSeconds+"毫秒");

/*
Wed Apr 11 16:18:42 CST 2012
Wed Apr 11 16:18:42 +0800 2012
2012-04-11 16:18:42
2012年04月11日 16时18分42秒
自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象经过的毫秒数为:1334132322000毫秒
*/
}
}

❽ 用java如何取得从1970 年到现在的毫秒数

给你一段代码作为参考,我想你只要调整这个方法即可:

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

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime=new Date();
//将截取到的时间字符串转化为时间格式的字符串
Date beginTime=sdf.parse("1970-01-01 12:53:30");
//默认为毫秒
long interval=(currentTime.getTime()-beginTime.getTime());
}

❾ java时间转换

java中毫秒转日期:
//毫秒转换为日期
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println(now + " = " + formatter.format(calendar.getTime()));
// 日期转换为毫秒 两个日期想减得到天数
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start="2011-09-20 12:30:45";
String end ="2011-10-20 6:30:00";
//得到毫秒数
long timeStart=sdf.parse(start).getTime();
long timeEnd =sdf.parse(end).getTime();
//两个日期想减得到天数
long dayCount= (timeEnd-timeStart)/(24*3600*1000);
System.out.println(dayCount);
}

❿ java 怎么获取指定时间的毫秒值如(2012-5-5)

DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try {
java.util.Date dateTime = format.parse("2012-05-05");
long time=dateTime.getTime();
System.out.println("Time:"+time);
} catch (ParseException e) {
e.printStackTrace();
}
时间如果是Date类型直接getTime()就可以...如果是String类型就用上面的代码转成Date然后取毫秒值.

阅读全文

与java时间转换毫秒相关的资料

热点内容
自动获取文件夹 浏览:513
在那个文件夹找到做同款 浏览:895
androidpem证书 浏览:574
apache安装教程linux 浏览:980
安卓手机的三种锁都是什么锁 浏览:123
编程猫的优质同人通关 浏览:238
计算机编程二级考试成绩查询 浏览:878
linux进入mysql数据库命令 浏览:834
英语早教app哪个好 浏览:916
程序员考试大纲2022 浏览:751
31岁程序员的感受 浏览:790
30程序员转行教师 浏览:774
新笑傲江湖手游充值哪个app好 浏览:489
sql权威指南pdf 浏览:260
定位书哪个app可以看 浏览:205
微博程序员可以查看用户登录名吗 浏览:358
小米自带的解压失败 浏览:535
python中分支结构能写循环 浏览:576
linux第二章 浏览:587
app启动失败是什么原因 浏览:880