`

date格式化

    博客分类:
  • java
 
阅读更多
/**
  * 取得前一天的日期
  *
  * @return
  */
 public static String getStringDate() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Calendar cal = Calendar.getInstance();
  // 取得当前日期的前一天
  cal.add(Calendar.HOUR_OF_DAY, -24);
  return sdf.format(cal.getTime());
 }

 /**
  * 当前月的上个月的第一天
  *
  * @return
  */
 public static String getPreviousMonthFirst() {
  String str = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Calendar lastDate = Calendar.getInstance();
  // 设为当前月的1号
  lastDate.set(Calendar.DATE, 1);
  // 减一个月,变为上月的1号
  lastDate.add(Calendar.MONTH, -1);
  str = sdf.format(lastDate.getTime());
  return str;
 }

 /**
  * 当前月的上个月的最后一天
  *
  * @return
  */
 public static String getPreviousMonthEnd() {
  String str = "";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Calendar lastDate = Calendar.getInstance();
  // 减一个月
  lastDate.add(Calendar.MONTH, -1);
  // 把日期设置为当月第一天
  lastDate.set(Calendar.DATE, 1);
  // 日期回滚一天,也就是上个月最后一天
  lastDate.roll(Calendar.DATE, -1);
  str = sdf.format(lastDate.getTime());
  return str;
 }

 /**
  * 取得指定时间的前count个月的第一天
  *
  * @return
  */
 public static String getPreviousMonthFirst(Date date, String format,
   int count) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  Calendar lastDate = Calendar.getInstance();
  lastDate.setTime(date);
  // 设为当前月的1号
  lastDate.set(Calendar.DATE, 1);
  // 减一个月,变为上月的1号
  lastDate.add(Calendar.MONTH, count);
  return sdf.format(lastDate.getTime());
 }

 /**
  * 取得指定时间的前count个月最后一天
  *
  * @return
  */
 public static String getPreviousMonthEnd(Date date, String format, int count) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  Calendar lastDate = Calendar.getInstance();
  lastDate.setTime(date);
  // 减count个月
  lastDate.add(Calendar.MONTH, count);
  // 把日期设置为当月第一天
  lastDate.set(Calendar.DAY_OF_MONTH, 1);
  // 日期回滚一天,也就是上个月最后一天
  lastDate.roll(Calendar.DAY_OF_MONTH, -1);
  return sdf.format(lastDate.getTime());
 }

 /**
  * 将Date转换成String
  *
  * @param date
  * @return
  */
 public static String getStringDate(Date date) {
  // 日期格式
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  return sdf.format(date);
 }

 /**
  * 将Date转换成String
  *
  * @param date
  * @param format
  * @return
  */
 public static String getStringDate(Date date, String format) {
  // 日期格式
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  return sdf.format(date);
 }

 /**
  * 字符转成日期
  *
  * @param dateString
  * @return
  * @throws ParseException
  */
 public static Date getDate(String dateString) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  try {
   return sdf.parse(dateString);
  } catch (ParseException e) {
   e.printStackTrace();
   return null;
  }
 }

 /**
  * 字符转成日期
  *
  * @param dateString
  * @param format
  * @return
  * @throws ParseException
  */
 public static Date getDate(String dateString, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  try {
   return sdf.parse(dateString);
  } catch (ParseException e) {
   e.printStackTrace();
   return null;
  }
 }

 /**
  * 得到以当天时间为基础的指定日期
  *
  * @param field
  *            <Calendar.HOUR_OF_DAY>
  * @param amount
  *            <-24> 例 Calendar.HOUR_OF_DAY, -24
  * @return
  */
 public static Date getLatestDate(int field, int amount) {
  Calendar calendar = Calendar.getInstance();
  calendar.add(field, amount);
  return calendar.getTime();
 }

 /**
  * 取得两个时间段的时间间隔天数
  *
  * @param t1时间1
  * @param t2时间2
  * @return t2 与t1的间隔天数
  */
 public static int getBetweenDays(String t1, String t2) {
  int betweenDays = 0;
  try {
   DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
   Date d1 = format.parse(t1);
   Date d2 = format.parse(t2);
   Calendar c1 = Calendar.getInstance();
   Calendar c2 = Calendar.getInstance();
   c1.setTime(d1);
   c2.setTime(d2);
   // 保证第二个时间一定大于第一个时间
   if (c1.after(c2)) {
    c1 = c2;
    c2.setTime(d1);
   }
   int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
   betweenDays = c2.get(Calendar.DAY_OF_YEAR)
     - c1.get(Calendar.DAY_OF_YEAR);
   for (int i = 0; i < betweenYears; i++) {
    c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
    betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
   }
   return betweenDays;
  } catch (ParseException e) {
   e.printStackTrace();
   return 0;
  }
 }

 /**
  * 取得两个时间段的时间间隔月数
  *
  * @param t1时间1
  * @param t2时间2
  * @return t2 与t1的间隔天数
  */
 public static int getBetweenMonths(String t1, String t2) {
  int betweenDays = 0;
  try {
   DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
   Date d1 = format.parse(t1);
   Date d2 = format.parse(t2);
   Calendar c1 = Calendar.getInstance();
   Calendar c2 = Calendar.getInstance();
   c1.setTime(d1);
   c2.setTime(d2);
   // 保证第二个时间一定大于第一个时间
   if (c1.after(c2)) {
    c1 = c2;
    c2.setTime(d1);
   }
   int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
   betweenDays = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
   for (int i = 0; i < betweenYears; i++) {
    c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
    betweenDays += c1.getMaximum(Calendar.MONTH);
   }
   // 因为统计到年和月之间的总月数,需要+1
   return betweenDays + 1;
  } catch (ParseException e) {
   e.printStackTrace();
   return 0;
  }
 }

 /**
  * 返回指定日期格式的日期增加指定天数的日期
  *
  * @param appDate指定日期
  * @param format指定日期格式
  * @param days指定天数
  * @return 指定日期格式的日期增加指定天数的日期
  */
 public static String getFutureDay(String appDate, String format, int days) {
  String future = null;
  try {
   Calendar calendar = Calendar.getInstance();
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
   Date date = simpleDateFormat.parse(appDate);
   calendar.setTime(date);
   calendar.add(Calendar.DATE, days);
   date = calendar.getTime();
   future = simpleDateFormat.format(date);
  } catch (Exception e) {
   future = null;
   e.printStackTrace();
  }
  return future;
 }

 /**
  * 返回当前日期的前count个月的日期
  *
  * @param count倒退的月数
  * @param format格式
  * @return
  */
 public static String getPastMonthDay(int count, String format) {
  Calendar calendar = Calendar.getInstance();
  Date now = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  calendar.add(Calendar.MONTH, -count);
  now = calendar.getTime();
  return sdf.format(now);
 }
 
分享到:
评论

相关推荐

    Date格式化

    `Date`格式化在Java、JavaScript、Python等许多编程语言中都非常重要,因为正确处理日期和时间对于数据分析、日志记录、用户界面显示等功能至关重要。 在Java中,`java.util.Date`类和`SimpleDateFormat`类常用于...

    datetime类型用date格式化问题

    ### datetime类型用date格式化问题解析 #### 一、问题背景与概述 在处理日期时间相关的数据时,经常会遇到各种格式化的需求。特别是在PHP中,`datetime`类型的处理尤为重要,因为这直接影响到数据的正确性以及程序...

    Linux下date命令,格式化输出,时间设置方法

    ### Linux下的Date命令详解:格式化输出与时间设置 #### 一、Date命令概述 `date` 命令在Linux系统中是一个非常基础且重要的工具,主要用于显示或设置系统的日期与时间。通过灵活地使用该命令的不同选项,用户不仅...

    js通用日期格式化工具 date.js

    js通用日期格式化工具,直接引用该工具类就可以使用,方便快捷,建议大家收藏此工具类,该工具很好的解决了前端页面显示日期格式问题

    html5 date日期格式转换

    之前在做app,ios程序员要求将html5的日期(2015年10月15日转换为2015-10-15),这里了用户的错觉来实现,简单粗暴

    bootstap-talbe日期格式化

    在实际应用中,我们常常需要展示包含日期的数据,这就涉及到日期的格式化问题。本文将详细探讨如何在 Bootstrap Table 中对日期进行格式化,同时考虑到后端数据来源是 MySQL 数据库,并且使用 Spring MyBatis 作为...

    javascript中Date format(js日期格式化)方法小结.docx

    ### JavaScript中的Date Format(JS日期格式化)方法详解 #### 概述 在日常的Web开发工作中,我们经常需要处理日期和时间相关的数据。JavaScript 的 `Date` 对象提供了多种方法来获取和设置日期时间,但原生 API 并...

    PHP日期函数date格式化UNIX时间的方法

    本文实例讲述了PHP日期函数date格式化UNIX时间的方法。分享给大家供大家参考。具体分析如下: 日期函数可以根据指定的格式将一个unix时间格式化成想要的文本输出 使用到函数语法如下 string date (string $Format)...

    ajax请求时json时间格式的格式化显示

    3. `ajaxDataFormat.js`可能包含了`formatTime`函数,该函数可能使用JavaScript内置的`Date`对象来解析和格式化时间戳: ```javascript function formatTime(timestamp) { var date = new Date(timestamp * 1000); ...

    前端开源库-nunjucks-date

    1. **日期格式化**:nunjucks-date提供了与Moment.js兼容的日期格式化功能,允许开发者使用 Moment.js 的格式化语法在Nunjucks模板中进行日期转换。 2. **Moment.js集成**:如果你的项目中已经使用了Moment.js来...

    fortran-在fortran中对于Date和Time的操作.zip

    对于更复杂的日期和时间操作,如日期的加减、时间的格式化等,可能需要自定义函数或者使用第三方库,如Fortran 90后的`time`模块(非标准,但许多编译器支持)或现代的Fortran库如`Fortran.datetime`。 总的来说,...

    Node.js-dateformat-一个优秀的node.js日期格化包

    在 Node.js 中,原始的 `Date` 对象虽然能进行基本的日期操作,但其格式化的功能相对较弱。`dateformat` 模块则弥补了这一不足,通过简单的调用就能实现复杂的日期格式化。例如,你可以轻松地将日期转换为 "YYYY-MM-...

    JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss"

    在JavaScript中,获取和格式化当前时间是一项常见的任务,尤其在网页开发中,我们经常需要显示或处理日期和时间信息。下面将详细讲解如何利用JavaScript的内置Date对象以及自定义扩展来实现这一功能。 首先,我们...

    C#(ASP.NET)DateTime日期类型格式化显示

    1.绑定时格式化日期方法: &lt;ITEMSTYLE WIDTH= "18% " &gt; 2.数据控件如DataGrid/DataList等的件格式化日期方法: e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString(); 3.用String...

    javascript date格式化示例

    JavaScript中的Date对象是处理日期和时间的核心工具,它提供了丰富的功能来创建、操作和格式化日期。在JavaScript中,Date对象允许我们获取当前日期、设置特定日期、进行日期比较以及格式化日期输出。本文将深入探讨...

    js date 格式化

    本文将详细介绍如何使用JavaScript中的Date对象进行日期时间的格式化,重点以"yyyy-MM-dd HH:mm:ss"这一格式为例进行说明。 首先,我们需要知道JavaScript中获取当前日期和时间的方法是使用new Date()构造函数。...

    Js获取当前日期时间及格式化代码

    - 常见的日期时间格式化方法可以将Date对象转换为指定格式的字符串。 - `Date.prototype.isLeapYear` 方法判断是否为闰年。 - `Date.prototype.Format` 方法自定义格式化日期时间。 - 格式化中常用的是:YYYY...

    java完美按格式化字符串String转sql.date

    ### Java完美按格式化字符串String转sql.Date 在Java编程中,经常需要处理日期与时间相关的数据。特别是在数据库操作时,经常会遇到需要将字符串类型的日期转换为`java.sql.Date`类型的情况。本文将详细介绍如何...

Global site tag (gtag.js) - Google Analytics