欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学习Java的Date、Calendar日期操作

程序员文章站 2024-03-05 11:06:12
本文介绍了java开发过程中日期相关操作,分享的代码如下: package jse; import java.io.unsupportedencoding...

本文介绍了java开发过程中日期相关操作,分享的代码如下:

package jse;
 
import java.io.unsupportedencodingexception;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.locale;
 
/**
 * 常用日期操作
 * 
 * @author puck
 * createdate 2015-07-30 22:54:38
 */
public class testdate
{
  public static void main(string[] args) throws parseexception, unsupportedencodingexception
  {
    calendar cal = calendar.getinstance();
//   cal.add(calendar.day_of_month, -48);
    system.out.println(datetostring(cal));
  }
   
  /**
   * 日期格式化
   * @param date
   * @return
   */
  public static string datetostring(date date)
  {
//   simpledateformat format = new simpledateformat("y年mm月dd日 e hh时mm分ss秒", locale.china);
//   simpledateformat format = new simpledateformat("y年m月d日 e h时m分s秒", locale.china);
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss", locale.china); // example
    return sdf.format(date);
  }
   
  /**
   * 日期格式化
   * @param date
   * @return
   */
  public static string datetostring(calendar cal)
  {
    return datetostring(cal.gettime());
  }
 
  /**
   * datestring 转 calendar
   * 
   * @param date
   *      format:2015-06-16 date
   * @return calendar
   * @throws parseexception
   */
  public static calendar datestringtocalendar(string datestr) throws parseexception
  {
//   calendar cal = calendar.getinstance();
//   cal.clear();
//   cal.set(integer.parseint(date.substring(0, 4)), integer.parseint(date.substring(5, 7)) - 1,
//       integer.parseint(date.substring(8, 10)));
//   return cal;
     
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    date date = sdf.parse(datestr);
    calendar cal = calendar.getinstance();
    cal.settime(date);
    return cal;
  }
 
  /**
   * datestring 转 date
   * 
   * @param date
   *      format:yyyy-mm-dd hh:mm:ss date
   * @return calendar
   * @throws parseexception
   */
  public static date datestringtodate(string date) throws parseexception
  {
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    return sdf.parse(date);
  }
 
  /**
   * date convert to calendar
   * 
   * @param date
   * @return
   */
  public static calendar datetocalendar(date date)
  {
    calendar c1 = calendar.getinstance();
    c1.settime(date);
    return c1;
  }
 
  /**
   * calendar convert to date
   * @param cal
   * @return
   */
  public static date calendartodate(calendar cal)
  {
    return cal.gettime();
  }
 
  /**
   * 计算两个日期相差年月日
   * 
   * @param date
   *      c1
   * @param date
   *      c2
   * @return int[]{year, month, day}
   */
  public int[] calculatedifferday(date d1, date d2)
  {
    calendar c1 = calendar.getinstance();
    c1.settime(d1);
    calendar c2 = calendar.getinstance();
    c1.settime(d2);
    return calculatedifferday(c1, c2);
  }
 
  /**
   * 计算两个日期相差年月日
   * 
   * @param calendar
   *      c1
   * @param calendar
   *      c2
   * @return int[]{year, month, day}
   */
  public int[] calculatedifferday(calendar c1, calendar c2)
  {
    int[] p1 = { c1.get(calendar.year), c1.get(calendar.month), c1.get(calendar.day_of_month) };
    int[] p2 = { c2.get(calendar.year), c2.get(calendar.month), c2.get(calendar.day_of_month) };
    system.out.println("p1[0]=" + p1[0] + " p1[1]=" + p1[1] + " p1[2]=" + p1[2]);
    system.out.println("p2[0]=" + p2[0] + " p2[1]=" + p2[1] + " p2[2]=" + p2[2]);
    int year = p2[0] - p1[0];
    int month = (p2[0] * 12) + p2[1] - ((p1[0] * 12) + p1[1]);
    int day = (int) ((c2.gettimeinmillis() - c1.gettimeinmillis()) / (24 * 60 * 60 * 1000));
    return new int[] { year, month, day };
  }
 
  /**
   * 获取日期所在周的第一天
   * 
   * @param c
   * @return
   */
  public static calendar getlastdayofweek(calendar c)
  {
//   simpledateformat format2 = new simpledateformat("y年m月d日 e h时m分s秒", locale.china);
//   system.out.println("当前时间:" + format2.format(c.gettime()));
    c.set(calendar.day_of_week, calendar.monday);
//   system.out.println("周一时间:" + format2.format(c.gettime()));
    return c;
  }
 
  /**
   * 日期加减
   * @param c
   * @param day
   * @return
   */
  public static calendar addordecreaseday(calendar c, int day)
  {
    c.add(calendar.day_of_month, day);
    return c;
  }
 
  /**
   * 获取月最后一天
   * @param year
   * @param month
   * @return
   */
  public static int getlastdayofmonth(int year, int month)
  {
    calendar c = calendar.getinstance();
    c.set(year, month - 1, 1);
    return c.getactualmaximum(calendar.day_of_month);
  }
   
  /**
   * 获取月最后一天
   * @param cal
   * @return
   */
  public static int getlastdayofmonth(calendar cal)
  {
    return cal.getactualmaximum(calendar.day_of_month);
  }
   
}