//定义时区,可以避免虚拟机时间与系统时间不一致的问题
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒
String hourStr;
String minuteStr;
String secondStr;
if(hour < 10){
hourStr = "0"+hour;
}else{
hourStr = String.valueOf(hour);
}
if(minute < 10){
minuteStr = "0"+minute;
}else{
minuteStr = String.valueOf(minute);
}
if(second < 10){
secondStr = "0"+second;
}else{
secondStr = String.valueOf(second);
}
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒
String hourStr;
String minuteStr;
String secondStr;
if(hour < 10){
hourStr = "0"+hour;
}else{
hourStr = String.valueOf(hour);
}
if(minute < 10){
minuteStr = "0"+minute;
}else{
minuteStr = String.valueOf(minute);
}
if(second < 10){
secondStr = "0"+second;
}else{
secondStr = String.valueOf(second);
}