不能强迫日历来改变它的时间

时间:2015-11-05 12:30:33

标签: android parsing calendar simpledateformat

我试图获取当前日期并在20:00设置时间 我能够得到它我的代码没有设置时间,但也没有错误。如果我调试它的时间是当前的时间。你能帮助我吗?

 Calendar cal = Calendar.getInstance();
    String monthS = toString().valueOf(cal.get(Calendar.MONTH));
    String dayS = toString().valueOf(cal.get(Calendar.DAY_OF_MONTH));
  //  int hourI = cal.get(Calendar.HOUR_OF_DAY);
  //  int minI = cal.get(Calendar.MINUTE);
    String yearS = toString().valueOf(cal.get(Calendar.YEAR));






    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm yyyy");
    try
    {
        cal.setTime(sdf.parse(monthS + " " + dayS + " " + "20:00" + yearS));

    } catch (ParseException e)
    {
        e.printStackTrace();
    }

3 个答案:

答案 0 :(得分:2)

试试这个

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

答案 1 :(得分:2)

如果月份为X(1),则代替MMM尝试M,如果月份为XX(01)则为MM

MMM代表Jan...Dec

答案 2 :(得分:0)

我用这个

修复了它
Calendar cal = Calendar.getInstance();
    long currentMil = cal.getTimeInMillis();
    int hourI = cal.get(Calendar.HOUR_OF_DAY);

    while (hourI <19)
    {
        currentMil = currentMil + ONE_HOUR;
        hourI ++;
    }
    System.out.println(currentMil);