2012-01-02 119 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