導航:首頁 > 編程語言 > java日期轉換成毫秒

java日期轉換成毫秒

發布時間:2023-02-09 07:52:29

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字元串怎麼轉換成時間date格式,並把date再轉換成毫秒

樓上的轉化是不安全的 在中國的電腦上很多就執行不了。
應該這樣:
DateFormat df1 =
new SimpleDateFormat("dd-MMM-yy HH:mm",Locale.US);
//必須要指定本地的語言環境 否則 JUN 就無法解析,還有 記住 是3個M,2個就只能識別02,03這樣的數字元號。
DateFormat df2 =
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss:SS",Locale.CHINA);
//這個可以不指定語言環境
try {
Date dd = df1.parse("18-JUN-07 20:10"); //你要得到的Date日期
System.out.println(dd);
String str = df2.format(dd); //精確到毫秒的時間
System.out.println(str);
long str2 = dd.getTime(); //此date的毫秒數
System.out.println(str2);
} catch (ParseException e) {
System.out.println("輸入的日期格式有誤!");
}

Ⅲ Java 年月日字元串(如「20150806」)轉化為毫秒數 (時間戳 )

	try{
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Datedate=sdf.parse("20150806");
System.out.println(date.getTime());
}catch(Exceptione){
e.printStackTrace();
}

Ⅳ 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怎麼把時間轉化為毫秒值

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日期轉換為毫秒的數學公式

你幹嘛要手動計算呢?SDK 放那裡是讓你用的!

public long dateToLong (String in) {
SimpleDateFormat format = new SimpleDateFormat("y/M/d H:m:s");
Date date = format.parse(in);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTimeMillis();
}

拷去用吧

Ⅶ java中如何將Timestamp轉換為毫秒數

我寫了一個把當前時間轉換為毫秒數的例子,你參考一下,我這運行沒問題:

package test;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Administrator
*當前時間轉換為毫秒數
*/
public class DeclareTimer {

public static void main(String[] args) throws ParseException {
//獲取當前時間
Timestamp t = new Timestamp(new Date().getTime());
System.out.println("當前時間:"+t);
//定義時間格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String str = dateFormat.format(t);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
//此處轉換為毫秒數
long millionSeconds = sdf.parse(str).getTime();// 毫秒
System.out.println("毫秒數:"+millionSeconds);
}
}

Ⅷ java如何把時間格式轉為毫秒

獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。

一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化後再使用。

最常使用的就是,把String類型的日期先轉換為Date類型,最後直接調用.getTime()即可,這也是比較方便的了。

還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字元串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。

PS>

Ⅸ java date類型的數據怎麼能顯示到毫秒

使用java.sql包下的Timestamp(參數) 參數是時間戳 就可以顯示到毫秒

Ⅹ java語言中怎麼輸入日期後,轉為毫秒,就是用Scanner手動輸入的那種

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入日期(ep:2013-04-17):");
String time = scanner.nextLine();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(time);
System.out.println(date.getTime());
} catch (ParseException e) {
System.out.println("不合法的輸入");
e.printStackTrace();
}
}
}

閱讀全文

與java日期轉換成毫秒相關的資料

熱點內容
質數的後代python 瀏覽:148
如何做好美逛App 瀏覽:590
租伺服器租用後怎麼使用 瀏覽:284
木勺解壓助眠視頻 瀏覽:370
酒吧陪女電影 瀏覽:657
雙胞胎兄弟的韓國愛情電影 瀏覽:425
女人遇到網球教練韓國電影 瀏覽:43
一線影院和二線影院 瀏覽:379
韓國電影食物鏈裡面的女主角叫什麼名字? 瀏覽:461
程序員做開發256G夠用嗎 瀏覽:186
c程序編譯的秘密 瀏覽:178
安卓頁面廣告怎麼關閉 瀏覽:666
linux信號量命令 瀏覽:345
父親出門上班兒子和繼母的日本電影 瀏覽:954
安卓機怎麼摳鋼化膜 瀏覽:390
免費看影視網站7大入口 瀏覽:535
doctrinephp 瀏覽:752
dos命令ip 瀏覽:444
李彩譚的電影免費觀看 瀏覽:221
c語言編譯模擬登錄頁面 瀏覽:819