分享

时间,日期操作类 - baby69yy2000 - JavaEye技术网站

 WindySky 2008-06-12
  1. package com.aptech.util;   
  2.   
  3.   
  4. import java.text.ParseException;   
  5. import java.text.ParsePosition;   
  6. import java.text.SimpleDateFormat;   
  7. import java.util.Date;   
  8. import java.util.Calendar;   
  9. import java.util.GregorianCalendar;   
  10. import java.util.Locale;   
  11. import java.util.Random;   
  12. public class DateFormats {   
  13.        
  14.     public DateFormats() {   
  15.            
  16.     }   
  17.        
  18.      /**  
  19.      * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  
  20.      *  
  21.      * @param dateDate  
  22.      * @return 返回时间String类型 yyyy-MM-dd HH:mm:ss  
  23.      */  
  24.     public static String DateToStringDate(java.util.Date dateDate) {   
  25.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  26.         String dateString = formatter.format(dateDate);   
  27.         return dateString;   
  28.     }   
  29.        
  30.     /**  
  31.      * 获取现在时间  
  32.      *  
  33.      */  
  34.        
  35.     public static Date getCurrentDate() {   
  36.         Date currentDate = new Date();   
  37.         return currentDate;   
  38.     }   
  39.        
  40.        
  41.     /**  
  42.      * 获取现在时间  
  43.      *  
  44.      * @return 返回时间类型 yyyy-MM-dd HH:mm:ss  
  45.      */  
  46.     public static Date getNowDate() {   
  47.         Date currentTime = new Date();   
  48.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  49.         String dateString = formatter.format(currentTime);   
  50.         ParsePosition pos = new ParsePosition(8);   
  51.         Date currentTime_2 = formatter.parse(dateString, pos);   
  52.         return currentTime_2;   
  53.     }   
  54.        
  55.     /**  
  56.      * 获取现在时间  
  57.      *  
  58.      * @return返回短时间格式 yyyy-MM-dd  
  59.      */  
  60.     public static Date getNowDateShort() {   
  61.         Date currentTime = new Date();   
  62.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  63.         String dateString = formatter.format(currentTime);   
  64.         ParsePosition pos = new ParsePosition(8);   
  65.         Date currentTime_2 = formatter.parse(dateString, pos);   
  66.         return currentTime_2;   
  67.     }   
  68.        
  69.     /**  
  70.      * 获取现在时间  
  71.      *  
  72.      * @return返回字符串格式 yyyy-MM-dd HH:mm:ss  
  73.      */  
  74.     public static String getStringDate() {   
  75.         Date currentTime = new Date();   
  76.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  77.         String dateString = formatter.format(currentTime);   
  78.         return dateString;   
  79.     }   
  80.        
  81.     /**  
  82.      * 获取现在时间  
  83.      *  
  84.      * @return 返回短时间字符串格式yyyy-MM-dd  
  85.      */  
  86.     public static String getStringDateShort() {   
  87.         Date currentTime = new Date();   
  88.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  89.         String dateString = formatter.format(currentTime);   
  90.         return dateString;   
  91.     }   
  92.        
  93.     /**  
  94.      * 获取时间 小时:分;秒 HH:mm:ss  
  95.      *  
  96.      * @return  
  97.      */  
  98.     public static String getTimeShort() {   
  99.         SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");   
  100.         Date currentTime = new Date();   
  101.         String dateString = formatter.format(currentTime);   
  102.         return dateString;   
  103.     }   
  104.        
  105.     /**  
  106.      * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss  
  107.      *  
  108.      * @param strDate  
  109.      * @return  
  110.      */  
  111.     public static Date strToDateLong(String strDate) {   
  112.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  113.         ParsePosition pos = new ParsePosition(0);   
  114.         Date strtodate = formatter.parse(strDate, pos);   
  115.         return strtodate;   
  116.     }   
  117.     /**  
  118.      * 将短时间格式时间转换为字符串 yyyy-MM-dd  
  119.      *  
  120.      * @param dateDate  
  121.      * @param k  
  122.      * @return  
  123.      */  
  124.     public static String dateToStr(java.util.Date dateDate) {   
  125.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  126.         String dateString = formatter.format(dateDate);   
  127.         return dateString;   
  128.     }   
  129.        
  130.     /**  
  131.      * 将短时间格式字符串转换为时间 yyyy-MM-dd  
  132.      *  
  133.      * @param strDate  
  134.      * @return  
  135.      */  
  136.     public static Date strToDate(String strDate) {   
  137.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  138.         ParsePosition pos = new ParsePosition(0);   
  139.         Date strtodate = formatter.parse(strDate, pos);   
  140.         return strtodate;   
  141.     }   
  142.        
  143.     /**  
  144.      * 得到现在时间  
  145.      *  
  146.      * @return  
  147.      */  
  148.     public static Date getNow() {   
  149.         Date currentTime = new Date();   
  150.         return currentTime;   
  151.     }   
  152.        
  153.     /**  
  154.      * 提取一个月中的最后一天  
  155.      *  
  156.      * @param day  
  157.      * @return  
  158.      */  
  159.     public static Date getLastDate(long day) {   
  160.         Date date = new Date();   
  161.         long date_3_hm = date.getTime() - 3600000 * 34 * day;   
  162.         Date date_3_hm_date = new Date(date_3_hm);   
  163.         return date_3_hm_date;   
  164.     }   
  165.        
  166.        
  167.        
  168.     /**  
  169.      * 得到现在时间  
  170.      *  
  171.      * @return 字符串 yyyyMMdd HHmmss  
  172.      */  
  173.     public static String getStringToday() {   
  174.         Date currentTime = new Date();   
  175.         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");   
  176.         String dateString = formatter.format(currentTime);   
  177.         return dateString;   
  178.     }   
  179.        
  180. // 计算当月最后一天,返回字符串   
  181.     public String getDefaultDay(){   
  182.         String str = "";   
  183.         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   
  184.            
  185.         Calendar lastDate = Calendar.getInstance();   
  186.         lastDate.set(Calendar.DATE,1);//设为当前月的1号   
  187.         lastDate.add(Calendar.MONTH,1);//加一个月,变为下月的1号   
  188.         lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天   
  189.            
  190.         str=sdf.format(lastDate.getTime());   
  191.         return str;   
  192.     }   
  193.        
  194.     /**  
  195.      * 得到现在小时  
  196.      */  
  197.     public static String getHour() {   
  198.         Date currentTime = new Date();   
  199.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  200.         String dateString = formatter.format(currentTime);   
  201.         String hour;   
  202.         hour = dateString.substring(1113);   
  203.         return hour;   
  204.     }   
  205.        
  206.     /**  
  207.      * 得到现在分钟  
  208.      *  
  209.      * @return  
  210.      */  
  211.     public static String getTime() {   
  212.         Date currentTime = new Date();   
  213.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  214.         String dateString = formatter.format(currentTime);   
  215.         String min;   
  216.         min = dateString.substring(1416);   
  217.         return min;   
  218.     }   
  219.        
  220.     /**  
  221.      * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。  
  222.      *  
  223.      * @param sformat  
  224.      *              yyyyMMddhhmmss  
  225.      * @return  
  226.      */  
  227.     public static String getUserDate(String sformat) {   
  228.         Date currentTime = new Date();   
  229.         SimpleDateFormat formatter = new SimpleDateFormat(sformat);   
  230.         String dateString = formatter.format(currentTime);   
  231.         return dateString;   
  232.     }   
  233.        
  234.     /**  
  235.      * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟  
  236.      */  
  237.     public static String getTwoHour(String st1, String st2) {   
  238.         String[] kk = null;   
  239.         String[] jj = null;   
  240.         kk = st1.split(":");   
  241.         jj = st2.split(":");   
  242.         if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))   
  243.             return "0";   
  244.         else {   
  245.             double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;   
  246.             double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;   
  247.             if ((y - u) > 0)   
  248.                 return y - u + "";   
  249.             else  
  250.                 return "0";   
  251.         }   
  252.     }   
  253.        
  254.     /**  
  255.      * 得到二个日期间的间隔天数  
  256.      */  
  257.     public static String getTwoDay(String sj1, String sj2) {   
  258.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");   
  259.         long day = 0;   
  260.         try {   
  261.             java.util.Date date = myFormatter.parse(sj1);   
  262.             java.util.Date mydate = myFormatter.parse(sj2);   
  263.             day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);   
  264.         } catch (Exception e) {   
  265.             return "";   
  266.         }   
  267.         return day + "";   
  268.     }   
  269.        
  270.     /**  
  271.      * 时间前推或后推分钟,其中JJ表示分钟.  
  272.      */  
  273.     public static String getPreTime(String sj1, String jj) {   
  274.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  275.         String mydate1 = "";   
  276.         try {   
  277.             Date date1 = format.parse(sj1);   
  278.             long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;   
  279.             date1.setTime(Time * 1000);   
  280.             mydate1 = format.format(date1);   
  281.         } catch (Exception e) {   
  282.         }   
  283.         return mydate1;   
  284.     }   
  285.        
  286.     /**  
  287.      * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数  
  288.      */  
  289.     public static String getNextDay(String nowdate, String delay) {   
  290.         try{   
  291.             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  292.             String mdate = "";   
  293.             Date d = strToDate(nowdate);   
  294.             long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;   
  295.             d.setTime(myTime * 1000);   
  296.             mdate = format.format(d);   
  297.             return mdate;   
  298.         }catch(Exception e){   
  299.             return "";   
  300.         }   
  301.     }   
  302.        
  303.     /**  
  304.      * 判断是否润年  
  305.      *  
  306.      * @param ddate  
  307.      * @return  
  308.      */  
  309.     public static boolean isLeapYear(String ddate) {   
  310.            
  311.         /**  
  312.          * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年  
  313.          * 3.能被4整除同时能被100整除则不是闰年  
  314.          */  
  315.         Date d = strToDate(ddate);   
  316.         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();   
  317.         gc.setTime(d);   
  318.         int year = gc.get(Calendar.YEAR);   
  319.         if ((year % 400) == 0)   
  320.             return true;   
  321.         else if ((year % 4) == 0) {   
  322.             if ((year % 100) == 0)   
  323.                 return false;   
  324.             else  
  325.                 return true;   
  326.         } else  
  327.             return false;   
  328.     }   
  329.        
  330.     /**  
  331.      * 返回美国时间格式 26 Apr 2006  
  332.      *  
  333.      * @param str  
  334.      * @return  
  335.      */  
  336.     public static String getEDate(String str) {   
  337.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");   
  338.         ParsePosition pos = new ParsePosition(0);   
  339.         Date strtodate = formatter.parse(str, pos);   
  340.         String j = strtodate.toString();   
  341.         String[] k = j.split(" ");   
  342.         return k[2] + k[1].toUpperCase() + k[5].substring(24);   
  343.     }   
  344.        
  345.     /**  
  346.      * 获取一个月的最后一天  
  347.      *  
  348.      * @param dat  
  349.      * @return  
  350.      */  
  351.     public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd   
  352.         String str = dat.substring(08);   
  353.         String month = dat.substring(57);   
  354.         int mon = Integer.parseInt(month);   
  355.         if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {   
  356.             str += "31";   
  357.         } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {   
  358.             str += "30";   
  359.         } else {   
  360.             if (isLeapYear(dat)) {   
  361.                 str += "29";   
  362.             } else {   
  363.                 str += "28";   
  364.             }   
  365.         }   
  366.         return str;   
  367.     }   
  368.        
  369.     /**  
  370.      * 判断二个时间是否在同一个周  
  371.      *  
  372.      * @param date1  
  373.      * @param date2  
  374.      * @return  
  375.      */  
  376.     public static boolean isSameWeekDates(Date date1, Date date2) {   
  377.         Calendar cal1 = Calendar.getInstance();   
  378.         Calendar cal2 = Calendar.getInstance();   
  379.         cal1.setTime(date1);   
  380.         cal2.setTime(date2);   
  381.         int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);   
  382.         if (0 == subYear) {   
  383.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))   
  384.                 return true;   
  385.         } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {   
  386.             // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周   
  387.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))   
  388.                 return true;   
  389.         } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {   
  390.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))   
  391.                 return true;   
  392.         }   
  393.         return false;   
  394.     }   
  395.        
  396.     /**  
  397.      * 产生周序列,即得到当前时间所在的年度是第几周  
  398.      *  
  399.      * @return  
  400.      */  
  401.     public static String getSeqWeek() {   
  402.         Calendar c = Calendar.getInstance(Locale.CHINA);   
  403.         String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));   
  404.         if (week.length() == 1)   
  405.             week = "0" + week;   
  406.         String year = Integer.toString(c.get(Calendar.YEAR));   
  407.         return year + week;   
  408.     }   
  409.        
  410.     /**  
  411.      * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号  
  412.      *  
  413.      * @param sdate  
  414.      * @param num  
  415.      * @return  
  416.      */  
  417.     public static String getWeek(String sdate, String num) {   
  418.         // 再转换为时间   
  419.         Date dd = DateFormats.strToDate(sdate);   
  420.         Calendar c = Calendar.getInstance();   
  421.         c.setTime(dd);   
  422.         if (num.equals("1")) // 返回星期一所在的日期   
  423.             c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);   
  424.         else if (num.equals("2")) // 返回星期二所在的日期   
  425.             c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);   
  426.         else if (num.equals("3")) // 返回星期三所在的日期   
  427.             c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);   
  428.         else if (num.equals("4")) // 返回星期四所在的日期   
  429.             c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);   
  430.         else if (num.equals("5")) // 返回星期五所在的日期   
  431.             c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);   
  432.         else if (num.equals("6")) // 返回星期六所在的日期   
  433.             c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);   
  434.         else if (num.equals("0")) // 返回星期日所在的日期   
  435.             c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);   
  436.         return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
  437.     }   
  438.        
  439.     /**  
  440.      * 根据一个日期,返回是星期几的字符串  
  441.      *  
  442.      * @param sdate  
  443.      * @return  
  444.      */  
  445.     public static String getWeek(String sdate) {   
  446.         // 再转换为时间   
  447.         Date date = DateFormats.strToDate(sdate);   
  448.         Calendar c = Calendar.getInstance();   
  449.         c.setTime(date);   
  450.         // int hour=c.get(Calendar.DAY_OF_WEEK);   
  451.         // hour中存的就是星期几了,其范围 1~7   
  452.         // 1=星期日 7=星期六,其他类推   
  453.         return new SimpleDateFormat("EEEE").format(c.getTime());   
  454.     }   
  455.     public static String getWeekStr(String sdate){   
  456.         String str = "";   
  457.         str = DateFormats.getWeek(sdate);   
  458.         if("1".equals(str)){   
  459.             str = "星期日";   
  460.         }else if("2".equals(str)){   
  461.             str = "星期一";   
  462.         }else if("3".equals(str)){   
  463.             str = "星期二";   
  464.         }else if("4".equals(str)){   
  465.             str = "星期三";   
  466.         }else if("5".equals(str)){   
  467.             str = "星期四";   
  468.         }else if("6".equals(str)){   
  469.             str = "星期五";   
  470.         }else if("7".equals(str)){   
  471.             str = "星期六";   
  472.         }   
  473.         return str;   
  474.     }   
  475.        
  476.     /**  
  477.      * 两个时间之间的天数  
  478.      *  
  479.      * @param date1  
  480.      * @param date2  
  481.      * @return  
  482.      */  
  483.     public static long getDays(String date1, String date2) {   
  484.         if (date1 == null || date1.equals(""))   
  485.             return 0;   
  486.         if (date2 == null || date2.equals(""))   
  487.             return 0;   
  488.         // 转换为标准时间   
  489.         SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");   
  490.         java.util.Date date = null;   
  491.         java.util.Date mydate = null;   
  492.         try {   
  493.             date = myFormatter.parse(date1);   
  494.             mydate = myFormatter.parse(date2);   
  495.         } catch (Exception e) {   
  496.         }   
  497.         long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);   
  498.         return day;   
  499.     }   
  500.        
  501.     /**  
  502.      * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间  
  503.      * 此函数返回该日历第一行星期日所在的日期  
  504.      *  
  505.      * @param sdate  
  506.      * @return  
  507.      */  
  508.     public static String getNowMonth(String sdate) {   
  509.         // 取该时间所在月的一号   
  510.         sdate = sdate.substring(0,<IMG src="/images/smiles/icon_cool.gif">+ "01";   
  511.            
  512.         // 得到这个月的1号是星期几   
  513.         Date date = DateFormats.strToDate(sdate);   
  514.         Calendar c = Calendar.getInstance();   
  515.         c.setTime(date);   
  516.         int u = c.get(Calendar.DAY_OF_WEEK);   
  517.         String newday = DateFormats.getNextDay(sdate, (1 - u) + "");   
  518.         return newday;   
  519.     }   
  520.        
  521.     /**  
  522.      * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数  
  523.      *  
  524.      * @param k  
  525.      *              表示是取几位随机数,可以自己定  
  526.      */  
  527.        
  528.     public static String getNo(int k) {   
  529.            
  530.         return getUserDate("yyyyMMddhhmmss") + getRandom(k);   
  531.     }   
  532.        
  533.     /**  
  534.      * 返回一个随机数  
  535.      *  
  536.      * @param i  
  537.      * @return  
  538.      */  
  539.     public static String getRandom(int i) {   
  540.         Random random = new Random();   
  541.         if (i == 0)   
  542.             return "";   
  543.         String strRandom = "";   
  544.         for (int k = 0; k < i; k++) {   
  545.             strRandom = strRandom + random.nextInt(9);   
  546.         }   
  547.         return strRandom;   
  548.     }   
  549.        
  550.     /**  
  551.      *  
  552.      * @param args  
  553.      */  
  554.     public static boolean RightDate(String date) {   
  555.            
  556.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
  557.         ;   
  558.         if (date == null)   
  559.             return false;   
  560.         if (date.length() > 10) {   
  561.             sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
  562.         } else {   
  563.             sdf = new SimpleDateFormat("yyyy-MM-dd");   
  564.         }   
  565.         try {   
  566.             sdf.parse(date);   
  567.         } catch (ParseException pe) {   
  568.             return false;   
  569.         }   
  570.         return true;   
  571.     }   
  572.        
  573.        
  574.     public static String getNextMonthDay(String sdate, int m) {   
  575.           
  576.         int year = Integer.parseInt(sdate.substring(04));   
  577.         int month = Integer.parseInt(sdate.substring(57));   
  578.         month = month + m;   
  579.         if (month < 0) {   
  580.             month = month + 12;   
  581.             year = year - 1;   
  582.         } else if (month > 12) {   
  583.             month = month - 12;   
  584.             year = year + 1;   
  585.         }   
  586.         String smonth = "";   
  587.         if (month < 10)   
  588.             smonth = "0" + month;   
  589.         else  
  590.             smonth = "" + month;   
  591.         return year + "-" + smonth + "-10";   
  592.     }   
  593.     public static void main(String[] args) throws Exception {   
  594.            
  595.     }   
  596. }   


Java代码 复制代码
  1. package   test;    
  2.   
  3. import   java.text.ParseException;    
  4. import   java.text.SimpleDateFormat;    
  5. import   java.util.Calendar;    
  6. import   java.util.Date;    
  7.   
  8. public   class   TestD    
  9. {    
  10.   public   static   boolean   isSameYearMonth(Date   date1,   Date   date2)   {      
  11.           Calendar   cal1   =   Calendar.getInstance();      
  12.           Calendar   cal2   =   Calendar.getInstance();      
  13.           cal1.setTime(date1);      
  14.           cal2.setTime(date2);      
  15.           int   subYear   =   cal1.get(Calendar.YEAR)   -   cal2.get(Calendar.YEAR);      
  16.           int   subMonth=cal1.get(Calendar.MONTH)-cal2.get(Calendar.MONTH);    
  17.              
  18.           if   (0   ==   subYear&&   0==subMonth)    
  19.           return   true;    
  20.           return   false;      
  21.       }      
  22.   
  23. public   static   void   main(String[]   args)   throws   ParseException    
  24. {    
  25.         TestD   x=new   TestD   ();    
  26.         Date   x1=   new   Date();    
  27.         Date   x2=   new   Date();    
  28.         SimpleDateFormat   d   =   new   SimpleDateFormat("yyyy-MM-dd");    
  29.         x1=d.parse("2007-12-1");    
  30.         x2=d.parse("2007-12-31");    
  31.       if(   x.isSameYearMonth(x1,   x2))    
  32.       {    
  33.       System.out.println(x1.toString()+"和"+x2.toGMTString()+"同年同月");    
  34.       }    
  35.       else    
  36.       {    
  37.       System.out.println(x1.toLocaleString()+"和"+x2.toLocaleString()+"不同年同月");    
  38.       }    
  39.   
  40. }    
  41. }   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多