datetimeutils.java

来自「短信系统SMS:支持普通短信、长短信和wap push短信的发送。」· Java 代码 · 共 164 行

JAVA
164
字号
package com.ekun.common.util;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author ekun
 * @version 1.0
 */
import java.text.*;
import java.util.*;

public class DateTimeUtils
{
  public static int getCurrYear()
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(new Date());
    return ca.get(Calendar.YEAR);
  }

  public static int getCurrMonth()
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(new Date());
    return ca.get(Calendar.MONTH) + 1;
  }

  public static int getCurrDay()
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(new Date());
    return ca.get(Calendar.DAY_OF_MONTH);
  }

  public static int getYear(Date date)
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(date);
    return ca.get(Calendar.YEAR);
  }

  public static int getMonth(Date date)
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(date);
    return ca.get(Calendar.MONTH) + 1;
  }

  public static int getDay(Date date)
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(date);
    return ca.get(Calendar.DAY_OF_MONTH);
  }

  public static Date getBeforeDaysDate(int days)
  {
    return new Date(getCurrTime() - (long) days * 24 * 60 * 60 * 1000);
  }

  public static Date getAfterDaysDate(int days)
  {
    return new Date(getCurrTime() + (long) days * 24 * 60 * 60 * 1000);
  }

  public static long getCurrTime()
  {
    return new Date().getTime();
  }

  /**
   * 将long型的日期转换为字符型的日期
   * @param time long
   * @return String
   */
  public static String getDateTimeString(long time)
  {
    Date date = new Date(time);
    DateFormat df = new SimpleDateFormat();
    return df.format(date);
  }

  /**
   * 将long型的日期转换为字符型的日期,只保留年月日
   * @param time long
   * @return String
   */
  public static String getDateTimeStringWithYMD(long time)
  {
    Date date = new Date(time);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    return df.format(date);
  }

  public static String conString(String year, String month, String day)
  {
    String con = year + "-" + month + "-" + day;
    return con;
  }

  public static String conString(String year, String month, String day,
                                 String time)
  {
    String con = year + "-" + month + "-" + day + " " + time;
    return con;
  }

  public static long DateStringToLong1(String str)
  {
    DateFormat tempformat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    try
    {
      date = tempformat.parse(str);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return date.getTime();
  }

  public static long DateStringToLong2(String str)
  {
    DateFormat tempformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    try
    {
      date = tempformat.parse(str);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return date.getTime();
  }

  public static int DaysOfMonth(int m, int year)
  {
    if ( (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && (m == 2))
    {
      return 29;
    }
    else
    {
      if (m == 2)
      {
        return 28;
      }
      if ( (m == 4) || (m == 6) || (m == 9) || (m == 11))
      {
        return 30;
      }
    }
    return 31;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?