疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java中关于日期的操作


 

    列举Java中关于日期的基本操作,包括获取当前日期,某一天的前一天,某一天的后一天,某个月的总天数,某一天是星期几等。

  Java代码

  public class CalendaUtil {

  /**

  * 获取当前日期

  * @return String

  * @exception

  */

  public static String GetCurrentDate(){

  Date date = new Date();

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  return format.format(date);

  }

  /**

  * 获取某一天的前一天

  * @param specifiedDay

  * @return

  * @throws Exception

  */

  public static String getSpecifiedDayBefore(String specifiedDay){

  Calendar c = Calendar.getInstance();

  Date date = null;

  try {

  date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);

  } catch (ParseException e) {

  e.printStackTrace();

  }

  c.setTime(date);

  int day = c.get(Calendar.DATE);

  c.set(Calendar.DATE, day-1);

  String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

  return dayBefore;

  }

  /**

  * 获取某一天的后一天

  * @param specifiedDay

  * @return

  */

  public static String getSpecifiedDayAfter(String specifiedDay){

  Calendar c = Calendar.getInstance();

  Date date = null;

  try {

  date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);

  } catch (ParseException e) {

  e.printStackTrace();

  }

  c.setTime(date);

  int day = c.get(Calendar.DATE);

  c.set(Calendar.DATE, day+1);

  String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

  return dayAfter;

  }

  /**

  * 根據年月获取当月天数

  * @param year

  * @param month

  * @return int

  */

  public static int getDaysOfMonth(int year, int month){

  Calendar cal = Calendar.getInstance();

  cal.set(Calendar.YEAR, year);

  cal.set(Calendar.MONTH, month-1);

  int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

  return days;

  }

  /**

  * 格式化时间

  * @param year

  * @param month

  * @param day

  * @return Date

  */

  public static Date getFormatDate(int year, int month, int day) {

  String strDate = year + "-" + month + "-" + day;

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  try {

  return format.parse(strDate);

  } catch (ParseException e) {

  e.printStackTrace();

  }

  return null;

  }

  /**

  * 获取某一天是星期几

  * @param specifiedDay

  * @return String

  * @exception

  */

  public static String getWeek(String specifiedDay){

  Date date = null;

  try {

  date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);

  } catch (ParseException e) {

  e.printStackTrace();

  }

  SimpleDateFormat format = new SimpleDateFormat("EEEE");

  String week = format.format(date);

  return week;

  }

  }