java 年-月-日处理大全

  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.GregorianCalendar;  
  6. public class DateHelper {  
  7.     /* 
  8.     * 创建日期 2010-04-19 
  9.     * Author  sea.jiang 
  10.     * 
  11.     * 功能  取日期时间工具 
  12.     * 
  13.     */  
  14.     /** 
  15.     * 说明:      取日期时间工具  
  16.  
  17.     /** 
  18.     * @see    取得当前日期(格式为:yyyy-MM-dd) 
  19.     * @return String 
  20.     */  
  21.     public String GetDate()  
  22.     {  
  23.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  24.     String sDate = sdf.format(new Date());  
  25.     return sDate;  
  26.     }  
  27.   
  28.         /** 
  29.     * @see     取得当前时间(格式为:yyy-MM-dd HH:mm:ss) 
  30.     * @return String 
  31.     */  
  32.     public static String GetDateTime()  
  33.     {  
  34.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  35.     String sDate = sdf.format(new Date());  
  36.     return sDate;  
  37.     }  
  38.   
  39.         /** 
  40.     * @see     按指定格式取得当前时间() 
  41.     * @return String 
  42.     */  
  43.     public String GetTimeFormat(String strFormat)  
  44.     {  
  45.     SimpleDateFormat sdf = new SimpleDateFormat(strFormat);  
  46.     String sDate = sdf.format(new Date());  
  47.     return sDate;  
  48.     }  
  49.   
  50.         /** 
  51.     * @see     取得指定时间的给定格式() 
  52.     * @return String 
  53.     * @throws ParseException 
  54.     */  
  55.     public String SetDateFormat(String myDate,String strFormat) throws ParseException  
  56.     {  
  57.   
  58.             SimpleDateFormat sdf = new SimpleDateFormat(strFormat);  
  59.     String sDate = sdf.format(sdf.parse(myDate));  
  60.     System.err.println(sDate);  
  61.             return sDate;  
  62.     }  
  63.   
  64.         public String FormatDateTime(String strDateTime, String strFormat)  
  65.     {  
  66.     String sDateTime = strDateTime;  
  67.     try {  
  68.     Calendar Cal = parseDateTime(strDateTime);  
  69.     SimpleDateFormat sdf = null;  
  70.     sdf = new SimpleDateFormat(strFormat);  
  71.     sDateTime = sdf.format(Cal.getTime());  
  72.     } catch (Exception e) {  
  73.   
  74.             }  
  75.     return sDateTime;  
  76.     }  
  77.   
  78.         public static Calendar parseDateTime(String baseDate)  
  79.     {  
  80.     Calendar cal = null;  
  81.     cal = new GregorianCalendar();  
  82.     int yy = Integer.parseInt(baseDate.substring(04));  
  83.     int mm = Integer.parseInt(baseDate.substring(57)) - 1;  
  84.     int dd = Integer.parseInt(baseDate.substring(810));  
  85.     int hh = 0;  
  86.     int mi = 0;  
  87.     int ss = 0;  
  88.     if(baseDate.length() > 12)  
  89.     {  
  90.     hh = Integer.parseInt(baseDate.substring(1113));  
  91.     mi = Integer.parseInt(baseDate.substring(1416));  
  92.     ss = Integer.parseInt(baseDate.substring(1719));  
  93.     }  
  94.     cal.set(yy, mm, dd, hh, mi, ss);  
  95.     return cal;  
  96.     }  
  97.     public int getDay(String strDate)  
  98.     {  
  99.     Calendar cal = parseDateTime(strDate);  
  100.     return  cal.get(Calendar.DATE);  
  101.     }  
  102.   
  103.         public int getMonth(String strDate)  
  104.     {  
  105.     Calendar cal = parseDateTime(strDate);  
  106.   
  107.             return cal.get(Calendar.MONTH) + 1;  
  108.     }  
  109.   
  110.         public int getWeekDay(String strDate)  
  111.     {  
  112.     Calendar cal = parseDateTime(strDate);  
  113.     return cal.get(Calendar.DAY_OF_WEEK);  
  114.     }  
  115.   
  116.         public String getWeekDayName(String strDate)  
  117.     {  
  118.     String mName[] = {  
  119.     "日""一""二""三""四""五""六"  
  120.     };  
  121.     int iWeek = getWeekDay(strDate);  
  122.     iWeek = iWeek - 1;  
  123.     return "星期" + mName[iWeek];  
  124.     }  
  125.   
  126.         public int getYear(String strDate)  
  127.     {  
  128.     Calendar cal = parseDateTime(strDate);  
  129.     return cal.get(Calendar.YEAR) + 1900;  
  130.     }  
  131.   
  132.         public String DateAdd(String strDate, int iCount, int iType)  
  133.     {  
  134.     Calendar Cal = parseDateTime(strDate);  
  135.     int pType = 0;  
  136.     if(iType == 0)  
  137.     {  
  138.     pType = 1;  
  139.     } else  
  140.     if(iType == 1)  
  141.     {  
  142.     pType = 2;  
  143.     } else  
  144.     if(iType == 2)  
  145.     {  
  146.     pType = 5;  
  147.     } else  
  148.     if(iType == 3)  
  149.     {  
  150.     pType = 10;  
  151.     } else  
  152.     if(iType == 4)  
  153.     {  
  154.     pType = 12;  
  155.     } else  
  156.     if(iType == 5)  
  157.     {  
  158.     pType = 13;  
  159.     }  
  160.     Cal.add(pType, iCount);  
  161.     SimpleDateFormat sdf = null;  
  162.     if(iType <= 2)  
  163.     sdf = new SimpleDateFormat("yyyy-MM-dd");  
  164.     else  
  165.     sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  166.     String sDate = sdf.format(Cal.getTime());  
  167.     return sDate;  
  168.     }  
  169.   
  170.         public String DateAdd(String strOption, int iDays, String strStartDate)  
  171.     {  
  172.     if(!strOption.equals("d"));  
  173.     return strStartDate;  
  174.     }  
  175.   
  176.         public int DateDiff(String strDateBegin, String strDateEnd, int iType)  
  177.     {  
  178.     Calendar calBegin = parseDateTime(strDateBegin);  
  179.     Calendar calEnd = parseDateTime(strDateEnd);  
  180.     long lBegin = calBegin.getTimeInMillis();  
  181.     long lEnd = calEnd.getTimeInMillis();  
  182.     int ss = (int)((lBegin - lEnd) / 1000L);  
  183.     int min = ss / 60;  
  184.     int hour = min / 60;  
  185.     int day = hour / 24;  
  186.     if(iType == 0)  
  187.     return hour;  
  188.     if(iType == 1)  
  189.     return min;  
  190.     if(iType == 2)  
  191.     return day;  
  192.     else  
  193.     return -1;  
  194.     }  
  195.   
  196.     /***************************************** 
  197.     * @功能     判断某年是否为闰年 
  198.     * @return  boolean 
  199.     * @throws ParseException 
  200.     ****************************************/  
  201.     public boolean isLeapYear(int yearNum){  
  202.     boolean isLeep = false;  
  203.     /**判断是否为闰年,赋值给一标识符flag*/  
  204.     if((yearNum % 4 == 0) && (yearNum % 100 != 0)){  
  205.     isLeep = true;  
  206.     }  else if(yearNum % 400 ==0){  
  207.     isLeep = true;  
  208.     } else {  
  209.     isLeep = false;  
  210.     }  
  211.     return isLeep;  
  212.     }  
  213.   
  214.           
  215.     /***************************************** 
  216.     * @功能     计算当前日期某年的第几周 
  217.     * @return  interger 
  218.     * @throws ParseException 
  219.     ****************************************/  
  220.     public int getWeekNumOfYear(){  
  221.     Calendar calendar = Calendar.getInstance();  
  222.     int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);  
  223.     return iWeekNum;  
  224.     }  
  225.   
  226.         /***************************************** 
  227.     * @功能     计算指定日期某年的第几周 
  228.     * @return  interger 
  229.     * @throws ParseException 
  230.     ****************************************/  
  231.     public int getWeekNumOfYearDay(String strDate ) throws ParseException{  
  232.     Calendar calendar = Calendar.getInstance();  
  233.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  234.     Date curDate = format.parse(strDate);  
  235.     calendar.setTime(curDate);  
  236.     int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);  
  237.     return iWeekNum;  
  238.     }  
  239.     /***************************************** 
  240.     * @功能     计算某年某周的开始日期 
  241.     * @return  interger 
  242.     * @throws ParseException 
  243.     ****************************************/  
  244.     public String getYearWeekFirstDay(int yearNum,int weekNum) throws ParseException {  
  245.   
  246.     Calendar cal = Calendar.getInstance();  
  247.     cal.set(Calendar.YEAR, yearNum);  
  248.     cal.set(Calendar.WEEK_OF_YEAR, weekNum);  
  249.     cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
  250.     //分别取得当前日期的年、月、日  
  251.     String tempYear = Integer.toString(yearNum);  
  252.     String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);  
  253.     String tempDay = Integer.toString(cal.get(Calendar.DATE));  
  254.     String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;  
  255.     return SetDateFormat(tempDate,"yyyy-MM-dd");  
  256.   
  257.   
  258.   
  259.     }  
  260.     /***************************************** 
  261.     * @功能     计算某年某周的结束日期 
  262.     * @return  interger 
  263.     * @throws ParseException 
  264.     ****************************************/  
  265.     public String getYearWeekEndDay(int yearNum,int weekNum) throws ParseException {  
  266.     Calendar cal = Calendar.getInstance();  
  267.     cal.set(Calendar.YEAR, yearNum);  
  268.     cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);  
  269.     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
  270.     //分别取得当前日期的年、月、日  
  271.     String tempYear = Integer.toString(yearNum);  
  272.     String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);  
  273.     String tempDay = Integer.toString(cal.get(Calendar.DATE));  
  274.     String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;  
  275.     return SetDateFormat(tempDate,"yyyy-MM-dd");  
  276.     }  
  277.   
  278.   
  279.     /***************************************** 
  280.     * @功能     计算某年某月的开始日期 
  281.     * @return  interger 
  282.     * @throws ParseException 
  283.     ****************************************/  
  284.     public String getYearMonthFirstDay(int yearNum,int monthNum) throws ParseException {  
  285.   
  286.     //分别取得当前日期的年、月、日  
  287.     String tempYear = Integer.toString(yearNum);  
  288.     String tempMonth = Integer.toString(monthNum);  
  289.     String tempDay = "1";  
  290.     String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;  
  291.     return SetDateFormat(tempDate,"yyyy-MM-dd");  
  292.   
  293.     }  
  294.     /***************************************** 
  295.     * @功能     计算某年某月的结束日期 
  296.     * @return  interger 
  297.     * @throws ParseException 
  298.     ****************************************/  
  299.     public String getYearMonthEndDay(int yearNum,int monthNum) throws ParseException {  
  300.   
  301.            //分别取得当前日期的年、月、日  
  302.     String tempYear = Integer.toString(yearNum);  
  303.     String tempMonth = Integer.toString(monthNum);  
  304.     String tempDay = "31";  
  305.     if (tempMonth.equals("1") || tempMonth.equals("3") || tempMonth.equals("5") || tempMonth.equals("7") ||tempMonth.equals("8") || tempMonth.equals("10") ||tempMonth.equals("12")) {  
  306.     tempDay = "31";  
  307.     }  
  308.     if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9")||tempMonth.equals("11")) {  
  309.     tempDay = "30";  
  310.     }  
  311.     if (tempMonth.equals("2")) {  
  312.     if (isLeapYear(yearNum)) {  
  313.     tempDay = "29";  
  314.     } else {  
  315.     tempDay = "28";  
  316.     }  
  317.     }  
  318.     //System.out.println("tempDay:" + tempDay);  
  319.     String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;  
  320.     return SetDateFormat(tempDate,"yyyy-MM-dd");  
  321.   
  322.         }  
  323.   
  324.     public static void main(String args[]) throws ParseException  
  325.     {  
  326.         DateHelper dh = new DateHelper();  
  327.         dh.getYearWeekFirstDay(2012,8);  
  328.         dh.getYearWeekEndDay(2012,8);  
  329.           
  330.     }  
  331. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值