2013-03-05 27 views
0

如何將日期的標準輸出轉換爲分鐘數?如何將標準日期轉換爲java分鐘?

我有輸出從我的命令,如:

週一03月04日十二時33分58秒2013

,我需要把它轉換成若干分鐘說minutes1

,因爲我有這給分鐘數爲當前時間的代碼和代碼是:

public class DateToMinutes { 

    public static void main(String[] args) { 

     Date date = new Date(); 

     long now = ((date.getTime())/(60000)); 

     System.out.println("Total Minutes are " + now); 

    } 

} 

的這個輸出將在幾分鐘內說minutes2

我需要在兩個minutes1minutes2比較。

,因爲我無法標準的日期轉換爲minutes1,它現在是不可能的。

+0

看的SimpleDateFormat的解析方法http://docs.oracle.com/javase /1.4.2/docs/api/java/text/SimpleDateFormat.html – BigMike 2013-03-05 10:56:46

回答

3

看看SimpleDateFormat的解析方法,你會發現用於轉換日期的格式。

Javadocs here

在你的情況是這樣的應該工作:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); 
Date theDate = sdf.parse ("Mon Mar 4 12:33:58 2013"); 
long minutes2 = theDate.getTime()/60000; 
0

使用simpledate格式化。

DateFormat formatter = new SimpleDateFormat("mm"); 
Date one = ... 
Date two = ... 
formatter.format(one)); // only the minutes remain 
formatter.format(two)); // only the minutes remain 

// do whatever you want. 
0

確定minutes1是解析日期並將其轉變。

相關問題,可以發現herehere

此基礎上你的日期格式的描述應該做的工作:

SimpleDateFormat yourStandardDateFormat = new SimpleDateFormat("EEE MMM dd kk:mm:ss yyyy"); 
Date date = yourStandardDateFormat.parse(stringRepresentingDate); 
long minutes1 = date.getTime()/60000l; 

僅供參考,請參閱SimpleDateFormat documentation

相關問題