java date of acquisition, first of every month, every Monday

java date of acquisition, first of every month, every Monday 

  1.  
    package info.lumanman.h5.util;
  2.  
     
  3.  
    import java.text.SimpleDateFormat;
  4.  
    import java.util.Calendar;
  5.  
     
  6.  
    public class DateUtil {
  7.  
     
  8.  
         public static void main(String[] args) {
  9.  
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  10.  
             //addDay(sdf,10);
  11.  
             // getWeekDay (sdf);
  12.  
            getMonth(sdf);
  13.  
            
  14.  
        }
  15.  
         /**
  16.  
         * Increase the number of days
  17.  
         * @param sdf
  18.  
         */
  19.  
         public static void addDay(SimpleDateFormat sdf,int days){
  20.  
            
  21.  
             // Get Calendar example, java package performance, private constructor Calendar class object is created by the static method
  22.  
            Calendar calendar=Calendar.getInstance();
  23.  
             // get the current time and format
  24.  
            . The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
  25.  
            
  26.  
             // plus the number of days to the current time and outputs days, where the first parameter can be the following three
  27.  
            Calendar calendar1=Calendar.getInstance();
  28.  
            calendar1. add(Calendar.DAY_OF_YEAR, days);
  29.  
            . The System Out.println ( "plus" + Days + "time after day:" + sdf.format (calendar1.getTime () ));
  30.  
            
  31.  
            Calendar calendar2=Calendar.getInstance();
  32.  
            calendar2. add(Calendar.DAY_OF_MONTH, days);
  33.  
            . The System Out.println ( "plus" + Days + "time after:" + sdf.format (calendar2.getTime () ));
  34.  
            
  35.  
            Calendar calendar3=Calendar.getInstance();
  36.  
            calendar3. add(Calendar.DAY_OF_WEEK, days);
  37.  
            . The System Out.println ( "plus" + Days + "time after:" + sdf.format (calendar3.getTime () ));
  38.  
        }
  39.  
         /**
  40.  
         * Get the current week, a week ago, after the first day and the last day of the week
  41.  
         * @param sdf
  42.  
         */
  43.  
         public static void getWeekDay(SimpleDateFormat sdf){
  44.  
             // Get Calendar example, java package performance, private constructor Calendar class object is created by the static method
  45.  
            Calendar calendar=Calendar.getInstance();
  46.  
             // get the current time and format
  47.  
            . The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
  48.  
            
  49.  
             // Monday
  50.  
            Calendar calendar1=Calendar.getInstance();
  51.  
            calendar1. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  52.  
            System. Out.println ( "Monday:" + sdf.format (calendar1.getTime () ));
  53.  
             // Sunday
  54.  
            Calendar calendar2=Calendar.getInstance();
  55.  
             // foreign Sunday and we are not one week (Sunday through Saturday are foreign to one week)
  56.  
            calendar2. add(Calendar.DAY_OF_WEEK, 7);
  57.  
            calendar2. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  58.  
            . System out.println ( "Sunday:" + sdf.format (calendar2.getTime () ));
  59.  
            
  60.  
             //Next Mon
  61.  
            Calendar calendar3=Calendar.getInstance();
  62.  
            calendar3. add(Calendar.DAY_OF_MONTH, 7);
  63.  
            calendar3. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  64.  
            . The System Out.println ( "Monday:" + sdf.format (calendar3.getTime () ));
  65.  
             // next Sunday
  66.  
            Calendar calendar4=Calendar.getInstance();
  67.  
            calendar4. add(Calendar.DAY_OF_MONTH, 14);
  68.  
            calendar4. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  69.  
            . System out.println ( "next Sunday:" + sdf.format (calendar4.getTime () ));
  70.  
            
  71.  
             //last Monday
  72.  
            Calendar calendar5=Calendar.getInstance();
  73.  
            calendar5. add(Calendar.DAY_OF_YEAR, -7);
  74.  
            calendar5. set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  75.  
            . System out.println ( "last Monday:" + sdf.format (calendar5.getTime () ));
  76.  
             // on Sunday
  77.  
            Calendar calendar6=Calendar.getInstance();
  78.  
            calendar6. set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  79.  
            . System out.println ( "on Sunday:" + sdf.format (calendar6.getTime () ));
  80.  
        }
  81.  
         /**
  82.  
         * Get the current month, the previous month, the first day after the last day of the month
  83.  
         * @param sdf
  84.  
         */
  85.  
         public static void getMonth(SimpleDateFormat sdf){
  86.  
             // define the total number of days of the current month, such as 30,31,28,29
  87.  
             int maxCurrentMonthDay=0;
  88.  
            Calendar calendar=Calendar.getInstance();
  89.  
            . The System Out.println ( "Current Time:" + sdf.format (calendar.getTime () ));
  90.  
            
  91.  
             // One month
  92.  
            Calendar calendar1=Calendar.getInstance();
  93.  
            calendar1. set(Calendar.DAY_OF_MONTH, 1);
  94.  
            . The System Out.println ( "the first day of the month:" + sdf.format (calendar1.getTime () ));
  95.  
             // the last day of the month
  96.  
            Calendar calendar2=Calendar.getInstance();
  97.  
            maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
  98.  
            calendar2. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
  99.  
            . System out.println ( "the last day of this month:" + sdf.format (calendar2.getTime () ));
  100.  
            
  101.  
             // One month
  102.  
            Calendar calendar3=Calendar.getInstance();
  103.  
            maxCurrentMonthDay=calendar3.getActualMaximum(Calendar.DAY_OF_MONTH);
  104.  
            calendar3. add(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
  105.  
            calendar3. set(Calendar.DAY_OF_MONTH, 1);
  106.  
            . The System Out.println ( "the first day of the next month:" + sdf.format (calendar3.getTime () ));
  107.  
             // the last day of next month
  108.  
            Calendar calendar4=Calendar.getInstance();
  109.  
            maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
  110.  
            calendar4. add(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
  111.  
             // The first maxCurrentMonthDay get the number of days of the month, the second maxCurrentMonthDay get the number of days of the next month
  112.  
            maxCurrentMonthDay=calendar4.getActualMaximum(Calendar.DAY_OF_MONTH);
  113.  
            calendar4. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
  114.  
            . The System Out.println ( "the first day of the next month:" + sdf.format (calendar4.getTime () ));
  115.  
            
  116.  
             // One month
  117.  
            Calendar calendar5=Calendar.getInstance();
  118.  
            maxCurrentMonthDay=calendar5.getActualMaximum(Calendar.DAY_OF_MONTH);
  119.  
            calendar5. add(Calendar.DAY_OF_MONTH, -maxCurrentMonthDay);
  120.  
            calendar5. set(Calendar.DAY_OF_MONTH, 1);
  121.  
            . The System Out.println ( "the first day of the month:" + sdf.format (calendar5.getTime () ));
  122.  
             // the last day of last month
  123.  
            Calendar calendar6=Calendar.getInstance();
  124.  
            maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
  125.  
            calendar6. add(Calendar.DAY_OF_MONTH, -maxCurrentMonthDay);
  126.  
             // The first maxCurrentMonthDay get the number of days of the month, the second maxCurrentMonthDay get the number of days last month
  127.  
            maxCurrentMonthDay=calendar6.getActualMaximum(Calendar.DAY_OF_MONTH);
  128.  
            calendar6. set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
  129.  
            . The System Out.println ( "the first day of the month:" + sdf.format (calendar6.getTime () ));
  130.  
            
  131.  
        }
  132.  }

Guess you like

Origin www.cnblogs.com/mayhh/p/11354818.html