我试图获取当前日期并在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();
}
答案 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);