Java时间就近取整<=30分向前取整点,>30分向后取整点

public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String time1="08:12";
String time2="07:56";
String time3="12:56";
String time4="23:56";

String time11=getInitialTime(time1);
String time22=getInitialTime(time2);
String time33=getInitialTime(time3);
String time44=getInitialTime(time4);

System.out.println(time11);
System.out.println(time22);
System.out.println(time33);
System.out.println(time44);
 

}

 

/**
* 时间就近取整:<=30分向前取整,>30分向后取整
* Author:zr
* @param time inTime 07:56
* @return outTime 08:00
*/

 

private static String getInitialTime(String time){
String hour="00";//小时
String minutes="00";//分钟
String outTime="00:00";
StringTokenizer st = new StringTokenizer(time, ":");
        List inTime = new ArrayList(); 
        while (st.hasMoreElements()) { 
        inTime.add(st.nextToken()); 
        }
        hour=inTime.get(0).toString();
        minutes=inTime.get(1).toString();
if(Integer.parseInt(minutes)>30){
hour=(Integer.parseInt(hour)+1)+"";
}        
outTime=hour+":00";
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");

try {
outTime=sdf.format(sdf.parse(outTime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outTime;
}

}

你可能感兴趣的:(Java,java,时间,取整,整点)