zoukankan      html  css  js  c++  java
  • Calendar.getInstance()获取指定时间点(定时)

    public class Test1 {
        public static void main(String[]args){
            System.out.println("时间为:
    "+getDate1()+"
    "+getDate2()+"
    "+getDate3()+"
    "+getDate4()+"
    "+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());
        }        
     
    }
  • 相关阅读:
    ArrayList源码解析
    Oracle WITH A AS 用法解析
    oracle关于时间分区的查询
    request.getRealPath("/")
    Oracle部分sql优化
    Java虚拟机
    java谜题-表达式谜题
    java和oracle简单的日期格式化
    java编译后在tomcat下无class文件
    JSP 中文乱码及其解决方案
  • 原文地址:https://www.cnblogs.com/diandianquanquan/p/10606960.html
Copyright © 2011-2022 走看看