Java中日期转换工具类--Java自学网_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java中日期转换工具类--Java自学网

Java中日期转换工具类--Java自学网

 2015/1/28 21:47:20  wurui8  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.util;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.*;importcom.ibm.icu.util.Calendar;/***日期转换工具*@authorAdministrator**/publicclassDateUtils{/***将日期形转为yyyy-MM
  • 标签:
package com.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ibm.icu.util.Calendar;
/**
* 日期转换工具
* @author Administrator
*
*/
public class DateUtils {
    /**
     * 将日期形转为yyyy-MM-dd形式
     *
     * @param date
     * @return
     */
    public static String date2String(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        } else {
            return "";
        }
    }
   
    /**
     * 将日期形转为yyyy-MM-dd形式
     *
     * @param date
     * @return
     */
    public static String date2Year(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            return sdf.format(date);
        } else {
            return "";
        }
    }
   
   
    /**
     * 将日期形转为MM-yy形式
     *
     * @param date
     * @return
     */
    public static String date2StringMMyy(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            return sdf.format(date);
        } else {
            return "";
        }
    }
    /**
     * 将日期对象转为中文的字符形式
     * @param date
     * @return
     */
    public static String date2ChineseString(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            return sdf.format(date);
        } else {
            return "";
        }
    }
    /**
     * 将日期转为yyyyMM的形式
     * @param date
     * @return
     */
    public static String date2String1(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            return sdf.format(date);
        } else {
            return "";
        }
    }
    /**
     * 将yyyy-MM-dd 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string2Date(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
   
    /**
     * 将yyyy-MM-dd HH:MM 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string2Date_All(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
    /**
     * 将yyyyMMdd 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string2Date_ymd(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
    /**
     * 将yyyy:MM:dd 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string2Date_y_m_d(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
   
    /**
     * 将yyyy-MM-dd 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string_YMD_Date(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
    public static Date string2Date2(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }
   
    /**
     * 将yyyy-MM-dd 类型的字符串转为日期
     *
     * @param dateStr
     * @return
     */
    public static Date string2Date1(String dateStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return null;
    }

    /**
     * 将日期分成年月日
     * @param date
     * @return
     */
    public static Map date2YMD(String dateStr){
     Map dateMap = new HashMap();
     if(dateStr != null){
      Date date = string2Date(dateStr);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH )+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH );
        dateMap.put("year", year+"");
      dateMap.put("month", month+"");
      dateMap.put("day", day+"");
      return dateMap;
     }else{
      dateMap.put("year", "");
      dateMap.put("month", "");
      dateMap.put("day", "");
      return dateMap;
     }
    }
   
    /**
     * 根据15、16原则返回年月
     * @param date
     * @return
     */
    public static Map date2YM(String dateStr){
     Map dateMap = new HashMap();
     if(dateStr != null){
      Date date = string2Date(dateStr);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int year1 = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH )+1;
        int day1 = calendar.get(Calendar.DAY_OF_MONTH );
        int year2 = 0;
        int month2 = 0;
        //year2计算后年
        if((month1+1)>12 && day1>15){year2=year1+1;}else{year2=year1;}
        //month2计算后月
        if(day1<=15){month2=month1;}
        else{
         if((month1+1)<=12){month2=month1+1;}
         else{month2=month1+1-12;}
        }
        dateMap.put("year", year2+"");
      dateMap.put("month", month2+"");
      return dateMap;
     }else{
      dateMap.put("year", "");
      dateMap.put("month", "");
      return dateMap;
     }
    }
   
    /**
     * 计算两个日期相差多少年,月,日
     * @param dateStrBegin
     * @param dateStrEnd
     * @return
     * @throws Exception
     */
    public static Map dateSubtract(String dateStrBegin,String dateStrEnd) throws Exception{

     Calendar cal = Calendar.getInstance();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(dateStrBegin);
        Date date2 = sdf.parse(dateStrEnd);
  cal.setTime(date1);
  Integer y1 = new Integer(cal.fieldDifference(date2, Calendar.YEAR));
  Integer m1 = new Integer(cal.fieldDifference(date2, Calendar.MONTH));
  Integer d1 = new Integer(cal.fieldDifference(date2, Calendar.DATE));
  Map fieldMap = new HashMap();
  fieldMap.put("year", y1);
  fieldMap.put("month", m1);
  fieldMap.put("day", d1);
  return fieldMap;
    }
    /**
     * 取得两个日期间隔时间
     * @param d1
     * @param d2
     * @return
     */
    public static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {
        if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end
            java.util.Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(java.util.Calendar.DAY_OF_YEAR) -
                   d1.get(java.util.Calendar.DAY_OF_YEAR);
        int y2 = d2.get(java.util.Calendar.YEAR);
        if (d1.get(java.util.Calendar.YEAR) != y2) {
            d1 = (java.util.Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
                d1.add(java.util.Calendar.YEAR, 1);
            } while (d1.get(java.util.Calendar.YEAR) != y2);
        }
        return days;
    }

   
    /**
     * 格式化日期为*年*月*日
     * @param dateStr
     * @return
     */
    public static String string2String(String dateStr) throws Exception{
        if (dateStr != null && !"".equals(dateStr)) {
         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            String dateString = dateFormat.format(date);
            return dateString;
        } else {
            return "";
        }
    }
    /**
     * 将时间转为0点0分0秒
     * @param d
     * @return
     */
    public static Date get0Time(Date d){
     if(d==null){
      return d;
     }
     Calendar c = Calendar.getInstance();
     c.setTime(d);
     c.set(Calendar.MILLISECONDS_IN_DAY,0);
     return c.getTime();
    }
   
    /**
     * 将时间转为23:59:59秒
     * @param d
     * @return
     */
    public static Date get24Time(Date d){
     if(d==null){
      return d;
     }
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(d);
  calendar.set(Calendar.HOUR_OF_DAY,23);
  calendar.set(Calendar.MINUTE,59);
  calendar.set(Calendar.SECOND,59);
  return calendar.getTime();
    }
   
    /**
     * 根据传入的年份和月份获得这个月的最大天数<br>
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMaxDaybyYearAndMonth(int year, int month) {
        int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (2 == month && 0 == (year % 4)
                && (0 != (year % 100) || 0 == (year % 400))) {
            days[1] = 29;
        }
        return (days[month - 1]);
    }


   
   
   
    /**
     * 当前时间加上天数后的时间
     * @param date
     * @param days
     * @return
     */
    public static Date getAddDate(Date date,int days){
      if(date==null){
        return null;
      }
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      c.add(Calendar.DAY_OF_YEAR,days);
      return c.getTime();
    }
   
    public static void main(String args[]) throws Exception{
    
    }

http://www.javalearns.com/
    /**
     * 取得当前年份
     * @return
     */
    public static int getCurrentYear(){
     Calendar calendar = Calendar.getInstance();
     return calendar.get(Calendar.YEAR);
    }
    /**
     * 取得当前月
     * @return
     */
    public static int getCurrentMonth(){
     Calendar calendar = Calendar.getInstance();
     return calendar.get(Calendar.MONTH)+1;
    }
   
    public static String getDateTimeString(Date d){
     if(d==null){
      return "";
     }
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
     return sdf.format(d);
    }
}
文章转载自  http://www.javalearns.com/Html/?1561.html

更多Java学习文章请访问  Java免费学习网 http://www.javalearns.com
  • 相关文章
发表评论
用户名: 匿名