Calendar.getInstance()獲取指定時間點(定時)


public class Test1 {
    public static void main(String[]args){
        System.out.println("時間為:\n"+getDate1()+"\n"+getDate2()+"\n"+getDate3()+"\n"+getDate4()+"\n"+getDate5());
    }
    
/*    Calendar.HOUR_OF_DAY     24小時制
    Calendar.HOUR     12小時制*/
    
    //獲取當天0點時間
    public static String getDate1(){
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.HOUR_OF_DAY, 0);//控制時
         cal.set(Calendar.MINUTE, 0);//控制分
         cal.set(Calendar.SECOND, 0);//控制秒
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         return sdf.format(cal.getTime());
    }
    
    //獲取當天12點時間
    public static String getDate2(){
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.HOUR_OF_DAY, 12);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         return sdf.format(cal.getTime());
    }    
    
    //獲取本周一0點時間
    public static String getDate3(){
         Calendar cal = Calendar.getInstance();
         cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
         cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         return sdf.format(cal.getTime());
    }    
    
    //獲取本月第一天0點時間
    public static String getDate4(){
         Calendar cal = Calendar.getInstance();
         cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
         cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         return sdf.format(cal.getTime());
    }
    
         //獲得本月最后一天24點時間
    public static String getDate5(){
         Calendar cal = Calendar.getInstance();
         cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
         cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
         cal.set(Calendar.HOUR_OF_DAY, 24);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         return sdf.format(cal.getTime());
    }        
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM