广告
返回顶部
首页 > 资讯 > 精选 >Java中如何对日期时间进行格式化
  • 425
分享到

Java中如何对日期时间进行格式化

javaava 2023-05-31 08:05:54 425人浏览 安东尼
摘要

这篇文章将为大家详细讲解有关Java中如何对日期时间进行格式化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Java格式化日期时间的方法import java.text.Parse

这篇文章将为大家详细讲解有关Java中如何对日期时间进行格式化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Java格式化日期时间的方法

import java.text.ParseException;import java.text.SimpleDateFORMat;import java.util.Calendar;import java.util.Date;import java.util.concurrent.TimeUnit;public class DateUtils {  private static final String[] UNIT_DESC = new String[]{"天", "小时", "分钟", "秒"};    public static String currentYYYYMMDD() {    return getStrByDate(new Date(), "yyyyMMdd");  }    public static String currentHHMMSS() {    return getStrByDate(new Date(), "HHmmss");  }    public static String currentYYYYMMDDHHmmss() {    return getStrByDate(new Date(), "yyyyMMddHHmmss");  }    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);  }    public static String getStrByDate(Date date, String format) {    assert date != null && format != null;    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);    return simpleDateFormat.format(date);  }    public static Date getDayOfMonth() {    Calendar now = Calendar.getInstance();    return now.getTime();  }    public static Date getFirstDayOfMonth(Date date) {    Calendar nowday = Calendar.getInstance();    nowday.setTime(date);    nowday.set(Calendar.DATE, 1);// 把日期设置为当月第一天    return nowday.getTime();  }    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();  }    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));  }    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));  }    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();  }    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);  }    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);  }    public static int getCurrYear() {    Calendar calendar = Calendar.getInstance();    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");    Date currYearFirst = calendar.getTime();    return Integer.valueOf(dateFormat.format(currYearFirst));  }    public static Date getLastThreeMonths() {    Calendar calendar = Calendar.getInstance();    calendar.add(Calendar.MONTH, -3);    calendar.add(Calendar.DAY_OF_MONTH, 1);    return calendar.getTime();  }    public static Date getLastOneMonths() {    Calendar calendar = Calendar.getInstance();    calendar.add(Calendar.MONTH, -1);    calendar.add(Calendar.DAY_OF_MONTH, 1);    return calendar.getTime();  }    public static Date getLastSixMonths() {    Calendar calendar = Calendar.getInstance();    calendar.add(Calendar.MONTH, -6);    calendar.add(Calendar.DAY_OF_MONTH, 1);    return calendar.getTime();  }    public static Date getCurrYearFirst(int year) {    Calendar calendar = Calendar.getInstance();    calendar.clear();    calendar.set(Calendar.YEAR, year);    return calendar.getTime();  }    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();  }    public static String date2Str(Date date, String format) {    return getStrByDate(date, format);  }    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;}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;}    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();  }    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中如何对日期时间进行格式化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

--结束END--

本文标题: Java中如何对日期时间进行格式化

本文链接: https://www.lsjlt.com/news/224527.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java中如何对日期时间进行格式化
    这篇文章将为大家详细讲解有关Java中如何对日期时间进行格式化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Java格式化日期时间的方法import java.text.Parse...
    99+
    2023-05-31
    java ava
  • Python3中怎么对日期进行格式化
    这篇文章主要介绍“Python3中怎么对日期进行格式化”,在日常操作中,相信很多人在Python3中怎么对日期进行格式化问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python3中怎么对日期进行格式化”的疑...
    99+
    2023-06-27
  • Java及数据库对日期进行格式化方式
    目录Java及数据库对日期进行格式化示例Java与数据库时间格式转换Java及数据库对日期进行格式化 Java对日期进行格式化可使用java.text.SimpleDateForma...
    99+
    2022-11-13
  • js中如何实现日期时间格式化
    这篇文章主要为大家展示了“js中如何实现日期时间格式化”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“js中如何实现日期时间格式化”这篇文章吧。js日期时间格式化将日期时间转换为指定格式,如:YY...
    99+
    2023-06-20
  • 怎么使用PHP date()函数对日期或时间进行格式化
    要使用PHP的date()函数对日期或时间进行格式化,需要传递两个参数给该函数。第一个参数是日期或时间的格式,第二个参数是要格式化的...
    99+
    2023-10-12
    PHP
  • python如何对日期时间进行处理
    这篇文章给大家分享的是有关python如何对日期时间进行处理的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。开发中常用的日期操作有哪些?获取当前时间获取系统秒数(从纪元时间开始)日期跟秒数之间转换获取日历等日期格式...
    99+
    2023-06-22
  • Java Date(日期)对象进行格式化的思路详解
    Java日期时间格式化的概念 我们在日常的开发过程中常常会碰到关于日期时间的计算与存储问题,比如我们要把一个当前时间类型转换成字符串类型,我们会直接使用Util包下的Date数据类型...
    99+
    2022-11-13
  • 在PHP中如何将时间戳转化为日期和时间格式
    本篇内容介绍了“在PHP中如何将时间戳转化为日期和时间格式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!什么是时间戳时间戳是指自1970年1...
    99+
    2023-07-05
  • dos中日期时间格式如何设置
    这篇文章将为大家详细讲解有关dos中日期时间格式如何设置,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。代码如下:@echo off @title 字符串的编辑测试(下面描述用的箭头→ ← 分别表示向右、向...
    99+
    2023-06-08
  • 如何在Java中使用SimpleDateFormat对日期格式进行转换
    这篇文章主要介绍了如何在Java中使用SimpleDateFormat对日期格式进行转换,编程网小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随编程网小编来看看吧!Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向...
    99+
    2023-05-30
    java simpledateformat
  • MySQL中的日期时间类型与格式化方式
    目录【1】mysql中的日期时间类型① 详细解释② SQL语句实例③ timestamp字段④ 测试实例【2】日期时间类型格式化 ① DATE_FORMAT( )函数② date_format( ) 转换格式③ str_...
    99+
    2022-07-04
    MySQL日期时间类型 MySQL日期格式化 时间日期类型
  • C#如何实现日期时间的格式化输出
    这篇文章主要讲解了“C#如何实现日期时间的格式化输出”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#如何实现日期时间的格式化输出”吧!DateTime被放在System命名空间下,在顶级语...
    99+
    2023-07-05
  • Laravel如何修改时间戳日期和时间格式
    小编给大家分享一下Laravel如何修改时间戳日期和时间格式,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!修改时间戳日期 / 时间格式以下内容引用官网文档 off...
    99+
    2023-06-27
  • 日期格式化的最佳实践:如何在Java中处理日期格式化
    文章目录 前言一、使用format()方法二、使用注解@JsonFormat三、使用消息转换器1.定义用户类2.重写DateSerializer 方法3.定义对象映射器:4.定义消息转换器5....
    99+
    2023-09-17
    java 开发语言
  • Java使用DateFormatter格式化日期时间的方法示例
    本文实例讲述了Java使用DateFormatter格式化日期时间的方法。分享给大家供大家参考,具体如下:Java版本:1.8开始import java.time.LocalDate;import java.time.LocalDateTi...
    99+
    2023-05-31
    java dateformatter 日期时间
  • Java格式化日期,时间(三种方法,建议收藏)
    1.String.format() 在java中String类格式化的方法,是静态format()用于创建格式化的字符串。 format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式...
    99+
    2023-08-31
    java
  • 如何使用PHP对日期进行格式化(三种方法)
    在开发网站或应用程序时,往往需要对日期进行格式化。有时候,我们需要把日期从格式如 2021-11-25 的年月日转换成格式如 11/25/2021 的月日年,以适应不同的需求和语言环境。在这篇文章中,我们将介绍如何使用PHP对日期进行格式化...
    99+
    2023-05-14
  • 在 Python 中对日期和时间进行排序
    文章目录 在 Python 中对日期和时间进行排序Python 中的日期时间模块sorted() 方法 使用 sorted() 方法对日期进行排序使用 sorted() 方法对时间进行排...
    99+
    2023-09-22
    python 开发语言
  • win7日期和时间格式如何更改
    这篇文章主要介绍“win7日期和时间格式如何更改”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“win7日期和时间格式如何更改”文章能帮助大家解决问题。win7日期和时间格式更改教程首先点击左下角,进...
    99+
    2023-07-01
  • 如何解决element-ui日期时间选择器的日期格式化问题
    小编给大家分享一下如何解决element-ui日期时间选择器的日期格式化问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!最近在...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作