java对时间的各种操作方法

    技术2022-05-11  11

    package com.tests;

    import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;

    public class LCalendar {

     /**  * 将短时间格式字符串转换为时间 yyyy-MM-dd  *   * @param strDate  * @return  */ public Date strToDate(String strDate) {  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  ParsePosition pos = new ParsePosition(0);  Date strtodate = formatter.parse(strDate, pos);  return strtodate; }

     /**  * 获取现在时间  *   * @return 返回短时间字符串格式yyyy-MM-dd  */ public String getStringDateShort() {  Date currentTime = new Date();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  String dateString = formatter.format(currentTime);  return dateString; }  /**   * 获取现在时间   *    * @return返回字符串格式 yyyy-MM-dd HH:mm:ss   */  public String getStringDate() {   Date currentTime = new Date();   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   String dateString = formatter.format(currentTime);   return dateString;  }

     /**  * @param nowdate 格式为:yyyy-MM-dd  * @param delay 整数 正数为后延天数,负数为前移天数  * @param 方法功能 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数  */ public String getNextDay(String nowdate, String delay) {

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  String mdate = "";  Date d = strToDate(nowdate);  long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60    * 60;  d.setTime(myTime * 1000);  mdate = format.format(d);  return mdate; }

     /**  * 根据日期获得这个日期所在的周的星期一和星期日的日期  *   * @param date  * @return  */ public String[] getWeekOfDate(String date) {  Calendar calendar = Calendar.getInstance();  calendar.setTime(strToDate(date));  int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;  String[] d = new String[2];  if (intWeek >= 1 && intWeek <= 6) {   if ((intWeek - 1) == 0) {    d[0] = date;    d[1] = getNextDay(date, "6");   } else {    d[0] = getNextDay(date, String.valueOf((1 - intWeek)));    d[1] = getNextDay(date, String.valueOf((7 - intWeek)));   }  } else {   d[0] = getNextDay(date, "-6");   d[1] = date;  }  return d; }

     /**  * 获得最近十五周  *   * @param date  * @return  */ public String getWeekOfYear() {  StringBuffer wk;  Calendar c = Calendar.getInstance();  int w = c.get(Calendar.WEEK_OF_YEAR);  String week = String.valueOf(w);  if (week.length() == 1)   week = "今年第0" + week + "周";  else   week = "今年第" + week + "周";

      Date currentTime = new Date();  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  String date = formatter.format(currentTime);  wk = new StringBuffer(week + "," + date);  for (int i = 1; i < 15; i++) {   date = getNextDay(date, "-7");   w--;   if (w < 1) {    c.setTime(strToDate(date));    int w1 = c.get(Calendar.WEEK_OF_YEAR);    String week1 = String.valueOf(w1);    if (week1.length() == 1)     week1 = "去年第0" + week1 + "周";    else     week1 = "去年第" + week1 + "周";    wk.append("," + week1 + "," + date);   } else {    week = String.valueOf(w);    if (week.length() == 1)     week = "今年第0" + week + "周";    else     week = "今年第" + week + "周";    wk.append("," + week + "," + date);   }  }  return wk.toString(); }

     /**  * 获得某一个日期所在的年的第几周  *   * @param date  * @return 返回格式如"2009年第05周"  */ public String getWeekYear(String date) {  Calendar c = Calendar.getInstance();  c.setTime(strToDate(date));  int year = c.get(Calendar.YEAR);  String y = String.valueOf(year);  int w = c.get(Calendar.WEEK_OF_YEAR);  String week = String.valueOf(w);  if (week.length() == 1)   week = y+"年第0" + week + "周";  else   week = y+"年第" + week + "周";

      return week; }  /**  * 获得某一个日期所在的月的第几周  *   * @param date  * @return 返回格式如"2009年02月第05周"  */ public String getWeekOfMonth(String date) {  Calendar c = Calendar.getInstance();  c.setTime(strToDate(date));  int year = c.get(Calendar.YEAR);  int month = c.get(Calendar.MONTH)+1;  String y = String.valueOf(year);  String m = String.valueOf(month);  if(m.length()==1)m = "0"+m;  int w = c.get(Calendar.WEEK_OF_MONTH);  String week = String.valueOf(w);  if (week.length() == 1)   week = y+"年"+m+"月第0" + week + "周";  else   week = y+"年"+m+"月第" + week + "周";

      return week; }  /**   * 得到二个日期之间的天数   */  public String getTwoDay(String sj1, String sj2) {   SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");   long day = 0;   try {    java.util.Date date = myFormatter.parse(sj2);    java.util.Date mydate = myFormatter.parse(sj1);    day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);   } catch (Exception e) {    return "";   }      return String.valueOf(day);  }    /**   * 判断是否润年   *    * @param ddate   * @return   */  public boolean isLeapYear(String ddate) {    /**    * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年    * 3.能被4整除同时能被100整除则不是闰年    */   Date d = strToDate(ddate);   GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();   gc.setTime(d);   int year = gc.get(Calendar.YEAR);   if ((year % 400) == 0)    return true;   else if ((year % 4) == 0) {    if ((year % 100) == 0)     return false;    else     return true;   } else    return false;  }    /**   * 取得一个月第一天的日期   *    * @param date   * @return   */    public String getFirstDayOfMonth(String date)  {  String a[] = date.split("-");  String day;   day = a[0]+"-"+a[1]+"-01";   return day;   }    /**   * 取得一个月最后一天的日期   *    * @param date   * @return   */    public String getLastDayOfMonth(String date)  {  String a[] = date.split("-");  int b = Integer.parseInt(a[1]);  String day;  if(b==2)  {   if(isLeapYear(date))    {     day = "29";    }else    {     day = "28";    }  }else if(b==2||b==4||b==6||b==9||b==11)  {   day = "30";  }  else   {   day = "31";  }    day = a[0]+"-"+a[1]+"-"+day;    return day;   }    /**   * 取得一个月有多少周   *    * @param date   * @return   */    public int getMonthOfWeeks(String date)  {   String lastDay = getLastDayOfMonth(date);   Calendar cal = Calendar.getInstance();   cal.setTime(strToDate(lastDay));   int weeks = cal.get(Calendar.WEEK_OF_MONTH);   return weeks;  }    /**   * 根据一个日期,返回是星期几的字符串   *    * @param sdate   * @return   */  public String getWeek(String sdate) {   // 再转换为时间   Date date = strToDate(sdate);   Calendar c = Calendar.getInstance();   c.setTime(date);   // int hour=c.get(Calendar.DAY_OF_WEEK);   // hour中存的就是星期几了,其范围 1~7   // 1=星期日 7=星期六,其他类推   return new SimpleDateFormat("EEEE").format(c.getTime());  }  public String getWeekStr(String sdate){   String str = "";   str = getWeek(sdate);   if("1".equals(str)){    str = "星期日";   }else if("2".equals(str)){    str = "星期一";   }else if("3".equals(str)){    str = "星期二";   }else if("4".equals(str)){    str = "星期三";   }else if("5".equals(str)){    str = "星期四";   }else if("6".equals(str)){    str = "星期五";   }else if("7".equals(str)){    str = "星期六";   }   return str;  }

    /* public static void main(String[] args) {  Calendar c = Calendar.getInstance();  LCalendar cl = new LCalendar();  String ca = getTwoDay("2009-02-06","2009-02-10");  System.out.println(ca); }*/

    }


    最新回复(0)