当前位置: 移动技术网 > IT编程>开发语言>Java > Java日期时间格式化操作DateUtils 的整理

Java日期时间格式化操作DateUtils 的整理

2019年07月19日  | 移动技术网IT编程  | 我要评论

java日期时间格式化操作dateutils 的整理

直接上代码,总结了开发过程中经常用到的日期时间格式化操作!

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.concurrent.timeunit;

/**
 * classname: dateutils <br/>
 * description:时间操作工具类
 */
public class dateutils {


  private static final string[] unit_desc = new string[]{"天", "小时", "分钟", "秒"};

  /**
   * 获得当前系统时间,格式为yyyymmdd
   *
   * @return 格式化后的时间
   */
  public static string currentyyyymmdd() {
    return getstrbydate(new date(), "yyyymmdd");
  }

  /**
   * 获得当前系统时间,格式为hhmmss
   *
   * @return 格式化后的时间
   */
  public static string currenthhmmss() {
    return getstrbydate(new date(), "hhmmss");
  }

  /**
   * 获得当前系统时间,格式为yyyymmddhhmmss
   *
   * @return 格式化后的时间
   */
  public static string currentyyyymmddhhmmss() {
    return getstrbydate(new date(), "yyyymmddhhmmss");
  }

  /**
   * 根据给定的字符串如:yyyy-mm-dd hh:mm:ss,(必须是这种格式) 返回一个日期日期形式
   *
   * @param strdate 要抛析的字符串,且字符串的形式必须:2007-09-10 07:00:00
   * @return 将字符串抛析成日期的格式返回
   * @throws parseexception 解析 format 字段失败
   */
  public static java.util.date getdatebystr(string strdate, string format) throws parseexception {
    assert strdate != null && format != null;
    simpledateformat simpledateformat = new simpledateformat(format);
    return simpledateformat.parse(strdate);
  }

  /**
   * 根据给定的日期,返回给定的字符串, 返回 字符串的形式是:yyyy-mm-dd hh:mm:ss
   *
   * @param date 要格式化的日期
   * @return 将日期格式化后返回的字符串,以这中格式返回:yyyy-mm-dd hh:mm:ss
   */
  public static string getstrbydate(date date, string format) {
    assert date != null && format != null;
    simpledateformat simpledateformat = new simpledateformat(format);
    return simpledateformat.format(date);
  }

  /**
   * 得到当前时间
   *
   * @return 当前时间
   */
  public static date getdayofmonth() {
    calendar now = calendar.getinstance();
    return now.gettime();
  }

  /**
   * 得到每月第一天
   *
   * @param date 日期
   * @return 日期月份的第一天
   */
  public static date getfirstdayofmonth(date date) {
    calendar nowday = calendar.getinstance();
    nowday.settime(date);
    nowday.set(calendar.date, 1);// 把日期设置为当月第一天
    return nowday.gettime();
  }

  /**
   * 得到每月最后一天
   *
   * @param date 日期
   * @return 日期月份最后一天
   */
  public static date getlastdayofmonth(date date) {
    calendar nowday = calendar.getinstance();
    nowday.settime(date);
    nowday.set(calendar.date, 1);// 把日期设置为当月第一天
    nowday.roll(calendar.date, -1);// 日期回滚一天,也就是最后一天
    return nowday.gettime();
  }

  /**
   * 获取当前年份 格式:yyyy
   *
   * @param date 当前时间
   * @return year
   */
  public static string getcurryear(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    simpledateformat dateformat = new simpledateformat("yyyy");
    date curryear = calendar.gettime();
    return string.valueof(dateformat.format(curryear));
  }

  /**
   * 获取当前月份 格式:mm
   *
   * @param date 当前时间
   * @return date
   */
  public static string getcurrmonth(date date) {
    calendar calendar = calendar.getinstance();
    calendar.settime(date);
    simpledateformat dateformat = new simpledateformat("mm");
    date currmonth = calendar.gettime();
    return string.valueof(dateformat.format(currmonth));
  }

  /**
   * 得到此日期的最后一天
   *
   * @param d 日期
   * @return 最后一天
   */
  public static date getlastdaybydate(date d) {
    calendar newday = calendar.getinstance();
    newday.settime(d);
    int lastday;
    int month = newday.get(calendar.month);
    do {
      lastday = newday.get(calendar.day_of_month);
      newday.add(calendar.day_of_month, 1);
    } while (newday.get(calendar.month) == month);
    newday.set(calendar.month, month);

    newday.set(calendar.day_of_month, lastday);
    return newday.gettime();
  }

  /**
   * 将 yyyymmdd 的字符窜 转化成 yyyy-mm-dd
   *
   * @param datestring yyyymmdd格式的日期
   * @return yyyy-mm-dd格式的日期
   * @throws parseexception
   */
  public static string formatyyyymmdd(string datestring) throws parseexception {
    simpledateformat simpledateformat = new simpledateformat("yyyymmdd");
    date date = simpledateformat.parse(datestring);
    simpledateformat formatstr = new simpledateformat("yyyy-mm-dd");
    return formatstr.format(date);
  }

  /**
   * 将 yyyymmdd 的字符窜 转化成 yyyy-mm-dd hh:mm:ss
   *
   * @param datestring
   * @return
   * @throws parseexception
   */
  public static string formatyyyymmddhhmmss(string datestring) throws parseexception {
    simpledateformat simpledateformat = new simpledateformat("yyyymmddhhmmss");
    date date = simpledateformat.parse(datestring);
    simpledateformat formatstr = new simpledateformat("yyyy-mm-dd hh:mm:ss");
    return formatstr.format(date);
  }

  /**
   * 获取当前年份 格式:yyyy
   *
   * @return date
   */
  public static int getcurryear() {
    calendar calendar = calendar.getinstance();
    simpledateformat dateformat = new simpledateformat("yyyy");
    date curryearfirst = calendar.gettime();
    return integer.valueof(dateformat.format(curryearfirst));
  }

  /**
   * 获取当前时间前三月
   *
   * @return date
   */
  public static date getlastthreemonths() {
    calendar calendar = calendar.getinstance();

    calendar.add(calendar.month, -3);
    calendar.add(calendar.day_of_month, 1);
    return calendar.gettime();
  }

  /**
   * 获取当前时间前一个月
   *
   * @return date
   */
  public static date getlastonemonths() {
    calendar calendar = calendar.getinstance();

    calendar.add(calendar.month, -1);
    calendar.add(calendar.day_of_month, 1);
    return calendar.gettime();
  }

  /**
   * 获取当前时间前六个月
   *
   * @return date
   */
  public static date getlastsixmonths() {
    calendar calendar = calendar.getinstance();

    calendar.add(calendar.month, -6);
    calendar.add(calendar.day_of_month, 1);
    return calendar.gettime();
  }

  /**
   * 获取某年第一天日期
   *
   * @param year 年份
   * @return date
   */
  public static date getcurryearfirst(int year) {
    calendar calendar = calendar.getinstance();
    calendar.clear();
    calendar.set(calendar.year, year);
    return calendar.gettime();
  }

  /**
   * 获取某年最后一天日期
   *
   * @param year 年份
   * @return date
   */
  public static date getcurryearlast(int year) {
    calendar calendar = calendar.getinstance();
    calendar.clear();
    calendar.set(calendar.year, year);
    calendar.roll(calendar.day_of_year, -1);
    return calendar.gettime();
  }

  /**
   * 格式化时间
   *
   * @param date  时间
   * @param format 格式化模板
   * @return 格式化后的时间
   */
  public static string date2str(date date, string format) {
    return getstrbydate(date, format);
  }
  
  

/**
 * 获得指定日期的前一天 yyyy-mm-dd
 * @param date
 * @return
 */
public static string getspecifieddaybefore(date date, string dateformat){
  if (date == null) return null;
  calendar c = calendar.getinstance();
  c.settime(date);
  int day=c.get(calendar.date);
  c.set(calendar.date,day-1);

  string daybefore=new simpledateformat(dateformat).format(c.gettime());
  return daybefore;
}

/**
 * 获得指定日期的后一天 yyyy-mm-dd
 *
 * @param date
 * @return
 */
public static string getspecifieddayafter(date date, string dateformat) {
  if (date == null) return null;
  calendar c = calendar.getinstance();
  c.settime(date);
  int day = c.get(calendar.date);
  c.set(calendar.date, day + 1);
  string dayafter = new simpledateformat(dateformat).format(c.gettime());
  return dayafter;
}

  /**
   * 格式化持续时间<br/>
   * 将持续时间,格式化为 xx天xx小时xx分钟xx秒 如果 "xx" 为0 自动缺省。
   *
   * @param seconds 持续时间,单位(秒)
   * @return 格式化后的字符串
   * @see timeunit 时间单位转换工具
   * @since 1.5
   */
  public static string convertseconds2str(long seconds) {
    stringbuilder sb = new stringbuilder();
    long[] date = {timeunit.seconds.todays(seconds), timeunit.seconds.tohours(seconds) % 24, timeunit.seconds.tominutes(seconds) % 60, timeunit.seconds.toseconds(seconds) % 60};
    for (int i = 0; i < date.length; i++) {
      long l = date[i];
      if (l > 0) sb.append(l).append(unit_desc[i]);
    }
    return sb.tostring();
  }

  /**
   * 格式化持续时间
   * 将持续时间,格式化为 xx天xx小时xx分钟xx秒 如果 "xx" 为0 自动缺省。
   *
   * @param seconds 持续时间,单位(分钟)
   * @return 格式化后的字符串
   * @see timeunit 时间单位转换工具
   * @since 1.5
   */
  public static string convertminute2str(long minute) {
    stringbuilder sb = new stringbuilder();
    long[] date = {timeunit.seconds.tohours(minute) % 24,timeunit.seconds.tominutes(minute) % 60, timeunit.seconds.toseconds(minute) % 60};
    for (int i = 0; i < date.length; i++) {
      long l = date[i];
      if (l > 0) sb.append(l).append(unit_desc[i]);
    }
    return sb.tostring();
  }
}

以上就是关于java 日期格式化操作的所有内容,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网