我尝试在0:00 am - 12:00 am的时间减去一天.
例如:2012-12-14 06:35 am=>2012-12-13
我做了一个功能,它的工作.但我的问题是在这种情况下还有其他更好的代码吗?更简单易懂.
public String getBatchDate() {
SimpleDateFormat timeFormatter = new SimpleDateFormat("H");
int currentTime = Integer.parseInt(timeFormatter.format(new Date()));
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
String Date = dateFormatter.format(new Date());
if ( 0 <= currentTime && currentTime <= 12){
try {
Calendar shiftDay = Calendar.getInstance();
shiftDay.setTime(dateFormatter.parse(Date));
shiftDay.add(Calendar.DATE, -1);
Date = dateFormatter.format(shiftDay.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("BatchDate:", Date);
}
return Date;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
Calendar shiftDay = Calendar.getInstance();
shiftDay.setTime(new Date())
if(shiftDay.get(Calendar.HOUR_OF_DAY) <= 12){
shiftDay.add(Calendar.DATE, -1);
}
//your date format
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1704 次 |
| 最近记录: |