Home > Java > javaTutorial > Date and Calendar date operations in Java

Date and Calendar date operations in Java

高洛峰
Release: 2017-01-20 16:34:26
Original
1077 people have browsed it

This article introduces date-related operations in the Java development process. The shared code is as follows:

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);
  }
    
}
Copy after login

For more articles related to Java's Date and Calendar date operations, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template