2012-01-02 180 views
2

我有一個與日期轉換/格式相關的問題。格式化日期和時間

我有一個日期,比如說,工作日期與一個值,例如:2011-11-27 00:00:00 從輸入文本框,我收到一個時間值(作爲字符串)的形式「HH:毫米:ss「,例如:」06:00:00「

我的任務是創建一個新的日期,比如newWorkDate,它具有與workDate相同的年份,月份,日期和時間作爲文本框輸入值。

所以在這種情況下,newWorkDate應該等於2011-11-27 06:00:00。 你能幫我弄清楚如何使用Java來實現這一點嗎?

這是我到目前爲止有:

SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
    //Text box input is converted to Date format -what will be the default year,month and date set here? 
    Date textBoxTime = df.parse(minorMandatoryShiftStartTimeStr); 

    Date workDate = getWorkDate(); 
    int year = Integer.parseInt(DateHelper.getYYYYMMDD(workDate).substring(0, 4)); 
    int month = Integer.parseInt(DateHelper.getYYYYMMDD(workDate).substring(4, 6)); 
    int date = Integer.parseInt(DateHelper.getYYYYMMDD(workDate).substring(6, 8)); 

    Date newWorkDate = DateHelper.createDate(year, month, day); 
    //not sure how to set the textBox time to this newWorkDate. 

[UPDATE]: Thx for the help,guys!Here is the updated code based on all your suggestions..Hopefully this will work.:) 

String[] split = textBoxTime.split(":"); 
      int hour = 0; 
       if (!split[0].isEmpty)){ 
       hour = Integer.parseInt(split[0]);} 
      int minute = 0; 
      if (!split[1].isEmpty()){ 
      minute = Integer.parseInt(split[1]);} 
      int second = 0; 
      if (!split[2].isEmpty()){ 
       second = Integer.parseInt(split[2]);} 

       Calendar cal=Calendar.getInstance(); 
       cal.setTime(workDate); 
       cal.set(Calendar.HOUR, hour); 
       cal.set(Calendar.MINUTE, minute); 
       cal.set(Calendar.SECOND, second); 
       Date newWorkDate = cal.getTime(); 
+0

我已經提出了包括問題本身在內的所有答案,因爲它們似乎都是正確的,值得一提的是獨一無二的。 – Lion 2012-01-02 06:34:38

+0

查看我上面的更新代碼.Thx – user656523 2012-01-02 06:37:08

回答

2

一對夫婦的提示:

  1. 使用Calendar對象與日期的工作。您可以從Date中設置Calendar,因此創建日期textBoxTimeworkDate的方式都可以。
  2. 使用上Calendar類的setXXX方法從textBoxTime設置的workDate值(請workDate一個Calendar
  3. 您可以使用SimpleDateFormat以及格式化的解析。使用它來產生所需的輸出。

你應該可以做到這一點,沒有字符串解析,只需幾行代碼。

+0

謝謝你的幫助! :) – user656523 2012-01-02 06:40:16

+0

只是好奇,我認爲你的前兩行是好的(使用SimpleDateFormatter解析 - 當然你需要一個try/catch向用戶報告錯誤),爲什麼不保留它們? – 2012-01-02 06:47:12

1

既然你已經有工作的日期,所有你需要做的是你的時間盒轉換成秒,並把它添加到你的約會對象。

將日曆用於日期算術。

Calendar cal=Calendar.getInstance(); 
cal.setTime(date); 
cal.add(Calendar.HOUR, hour); 
cal.add(Calendar.MINUTE, minute); 
cal.add(Calendar.SECOND, second); 
Date desiredDate = cal.getTime(); 
+1

Thankyou for this tip!我用它更新了代碼。 – user656523 2012-01-02 06:38:54

1

您可能需要下列代碼。

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 

public class DateFormat { 
    public static void main(String[] args) { 
     try { 
      SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd"); 
      Date workDate = simpleDateFormat1.parse("2011-11-27"); 
      Calendar workCalendar= Calendar.getInstance(); 
      workCalendar.setTime(workDate); 
      SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss"); 
      Calendar time = Calendar.getInstance(); 
      time.setTime(simpleDateFormat2.parse("06:00:00")); 
      workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY)); 
      workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE)); 
      workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND)); 
      Date newWorkDate = workCalendar.getTime(); 
      SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat(
        "yyyy-MM-dd hh:mm:ss"); 
      System.out.println(simpleDateFormat3.format(newWorkDate)); 
     } catch (NumberFormatException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

希望這會對你有所幫助。

+1

Ugggh ...這是很多解析,你應該讓SimpleDateFormatter處理。看到我的答案。 – 2012-01-02 06:28:48

+0

@Ahamed:謝謝你的詳細代碼,艾哈邁德!非常有用。這裏的小注意事項是文本框的值只給出了HHMMSS的值,所以不需要int年,月和日期。我可以直接設置工作日期的日曆,並用文本框覆蓋時間使用setXXX方法的值如Francis所建議的。 :) – user656523 2012-01-02 06:29:32

+0

@Francis,我同意你的看法。我改變了上面的代碼。 – Ahamed 2012-01-02 06:44:34