jdk groovy版本

我在诸如Groovy JDK(GDK):File.deleteDir() , Groovy JDK(GDK):Text File to String , Groovy JDK( )的博客文章中查看了Groovy GDK对Java JDK的扩展中可用的一些非常有用的方法。 GDK):更多文件乐趣 , Groovy JDK(GDK):字符串支持 ,和Groovy JDK(GDK):编号支持 。 在本文中,我将介绍Groovy对Java JDK java.util.Date和java.util.Calendar类的GDK扩展的一些实用功能。

Java开发社区通常不喜欢 Java当前对日期和时间的标准支持。 我们中的许多人都期待JSR-310和/或已经使用Joda Time来解决Java处理日期和时间的缺点。

当第三方框架不可用或无法使用时,Groovy使日期和时间的工作变得更容易.Groovy GDK Date扩展提供了几种新的且非常有用的方法,如其文档的屏幕快照所示。

我将在本文中重点介绍的这些有用的方法有: clearTime() , format(String) , getDateString() ,getTimeString(), parse(String,String) , parseToStringDate(String) , toCalendar() , toTimestamp()并更新(Map) 。 API中列出的许多其他方法都支持Groovy运算符重载 ,因此本文中不作重点介绍。

Date.clearTime()和Calendar.clearTime()

有时候,人们只希望表示一个日期,而“ Date或“ Calendar的时间部分并不重要(这正是JSR 310将仅日期的结构(如LocalDate)引入JDK 8的原因。 在这种情况下,Groovy对“ Date和“ Calendar的扩展使“时间”组件的清除变得很容易。 下一个代码清单演示了Date.clearTime()用法,然后是显示该代码已执行的屏幕快照。 请注意, clearTime()方法会将其作用的对象突变。

/*** Demonstrates Groovy's GDK Date.clearTime() method. Note that the clearTime()* method acts upon the Date object upon which it is called, mutating its value* in addition to returning a reference to that changed object.*/
def demoClearTime()
{printTitle('Groovy GDK Date.clearTime()')def now = new Date()println 'Now: ${now}'def timelessNow = now.clearTime()println 'Now sans Time: ${timelessNow}'println 'Mutated Time:  ${now}'
}

Calendar的clearTime()的工作方式与下一个代码片段及其执行过程的随附屏幕快照所示相似。

/*** Demonstrates Groovy's GDK Calendar.clearTime() method. Note that the* clearTime() method acts upon the Calendar object upon which it is called,* mutating its value in addition to returning a reference to that changed object.*/
def demoCalendarClearTime()
{printTitle('Groovy GDK Calendar.clearTime()')def now = Calendar.getInstance()println 'Now: ${now}'now.clearTime()println 'Now is Timeless: ${now}'
}

Date.format和Calendar.format

在Java开发中,通常需要以特定的用户友好格式显示DateCalendar ,这通常使用SimpleDateFormat实例来完成。 Groovy使用相应的Date.format(String)和Calendar.format(String)方法简化了将格式应用于DateString过程。 接下来显示了展示每个代码的代码清单,每个代码清单后面是显示执行代码的屏幕快照。

/*** Demonstrate how much more easily a formatted String representation of a Date* can be acquired in Groovy using GDK Date.format(String). No need for an* explicit instance of SimpleDateFormat or any other DateFormat implementation* here!*/
def demoFormat()
{printTitle('Groovy GDK Date.format(String)')def now = new Date()println 'Now: ${now}'def dateString = now.format('yyyy-MMM-dd HH:mm:ss a')println 'Formatted Now: ${dateString}'
}

/*** Demonstrate how much more easily a formatted String representation of a* Calendar can be acquired in Groovy using GDK Calendar.format(String). No need* for an explicit instance of SimpleDateFormat or any other DateFormat* implementation here!*/
def demoCalendarFormat()
{printTitle('Groovy GDK Calendar.format(String)')def now = Calendar.getInstance()println 'Now: ${now}'def calendarString = now.format('yyyy-MMM-dd HH:mm:ss a')println 'Formatted Now: ${calendarString}'
}

Date.getDateString(),Date.getTimeString()和Date.getDateTimeString()

先前显示的format方法允许自定义表示DateCalendar ,而clearTime方法则允许从DateCalendar实例中删除time元素。 Groovy在Date上提供了一些便利的方法,用于仅显示用户友好的日期,仅时间或日期和时间,而无需指定格式或清除时间部分。 这些方法以DateFormat.SHORT (用于日期部分)和DateFormat.MEDIUM (用于时间部分)指定的预定义格式打印日期和时间。 接下来显示每种方法的代码清单,每个代码清单之后是正在执行的代码的屏幕快照。

/*** Demonstrates Groovy's GDK Date.getDateString() method. Note that this* method doesn't change the underlying date, but simply presents only the date* portion (no time portion is presented) using the JDK's DateFormat.SHORT* constant (which defines the locale-specific 'short style pattern' for* formatting a Date).*/
def demoGetDateString()
{printTitle('Groovy GDK Date.getDateString()')def now = new Date()println 'Now: ${now}'println 'Date Only: ${now.getDateString()}'println 'Now Unchanged: ${now}'
}

/*** Demonstrates Groovy's GDK Date.getTimeString() method. Note that this* method doesn't change the underlying date, but simply presents only the time* portion (no date portion is presented) using the JDK's DateFormat.MEDIUM* constant (which defines the locale-specific 'medium style pattern' for* formatting a Date).*/
def demoGetTimeString()
{printTitle('Groovy GDK Date.getTimeString()')def now = new Date()println 'Now: ${now}'println 'Time Only: ${now.getTimeString()}'println 'Now Unchanged: ${now}'
}

/*** Demonstrates Groovy's GDK Date.getDateTimeString() method. Note that this* method doesn't change the underlying date, but simply presents the date and* time portions as a String. The date is presented with locale-specific format* as defined by DateFormat.SHORT and the time is presented with locale-specific* format as defined by DateFormat.MEDIUM.*/
def demoGetDateTimeString()
{printTitle('Groovy GDK Date.getDateTimeString()')def now = new Date()println 'Now: ${now}'println 'Date/Time String: ${now.getDateTimeString()}'println 'Now Unchanged: ${now}'
}

Date.parse(字符串,字符串)

GDK Date类提供了Date.parse(String,String)方法,该方法是一种“便捷方法”,“充当SimpleDateFormat的包装器”。 下面的代码段和相应的代码快照显示了该方法的有效性。

/*** Demonstrate Groovy GDK's Date.parse(String, String) method which parses a* String (second parameter) based on its provided format (first parameter).*/
def demoParse()
{printTitle('Groovy GDK Date.parse(String, String)')def nowString = '2012-Nov-26 11:45:23 PM'println 'Now String: ${nowString}'def now = Date.parse('yyyy-MMM-dd hh:mm:ss a', nowString)println 'Now from String: ${now}'
}

Date.parseToStringDate(String)

GDK Date.parseToStringDate(String)方法可用于从与Date.toString()方法提出的确切格式匹配的String获取Date的实例。 换句话说,该方法对于从DatetoString()方法生成的String转换回Date很有用。

下面的代码段和相应输出的屏幕快照演示了此方法的使用。

/*** Demonstrate Groovy GDK's Date.parseToStringDate(String) method which parses* a String generated by a Date.toString() call, but assuming U.S. locale to* do this.*/
def demoParseToStringDate()
{printTitle('Groovy GDK Date.parseToStringDate(String)')def now = new Date()println 'Now: ${now}'def nowString = now.toString()def nowAgain = Date.parseToStringDate(nowString)println 'From toString: ${nowAgain}'
}

GDK Date.parseToStringDate(String)方法有一个潜在的重大缺点。 如其文档所述,它仅依赖于“仅美国语言环境常量”。

Date.toCalendar()和Date.toTimestamp()

将java.util.Date转换为java.util.Calendar或java.sql.Timestamp通常很有用。 Groovy使用GDK Date提供的方法Date.toCalendar和Date.toTimestamp()使这些常见转换特别容易。 以下代码片段中演示了这些内容,其输出显示在相应的屏幕快照中。

/*** Demonstrates how easy it is to get a Calendar instance from a Date instance* using Groovy's GDK Date.toCalendar() method.*/
def demoToCalendar()
{printTitle('Groovy GDK Date.toCalendar()')def now = new Date()println 'Now: ${now}'def calendarNow = now.toCalendar()println 'Now: ${calendarNow} [${calendarNow.class}]'
}

/*** Demonstrates how easy it is to get a Timestamp instance from a Date instance* using Groovy's GDK Date.toTimestamp() method.*/
def demoToTimestamp()
{printTitle('Groovy GDK Date.toTimestamp()')def now = new Date()println 'Now: ${now}'def timestampNow = now.toTimestamp()println 'Now: ${timestampNow} [${timestampNow.class}]'
}

Date.updated(地图)[和Calendar.updated(地图)]

我将在本文中讨论的GDK Date提供的最终便利方法是Date.updated(Map) ,其文档描述为“支持创建具有与现有Date类似的新Date(保持不变)的新Date。但某些字段会根据变化图进行更新。” 换句话说,此方法允许一个以某个Date实例开始并获取另一个具有与提供的Map指定的更改不同的属性的Date实例。

下一个代码清单从现有的Date实例获取一个新的Date实例,并使用Date.updated(Map)方法更新了几个字段。 代码清单后是其执行的屏幕快照。

/*** Demonstrate Groovy GDK's Date.updated(Map) with adaptation of the example* provided for that method in that method's Javadoc-based GDK documentation.* Note that the original Date upon which updated is invoked is NOT mutated and* the updates are on the returned instance of Date only.*/
def demoUpdated()
{printTitle('Groovy GDK Date.updated(Map)')def now = new Date()def nextYear = now[YEAR] + 1def nextDate = now[DATE] + 1def prevMonth = now[MONTH] - 1def oneYearFromNow = now.updated(year: nextYear, date: nextDate, month: prevMonth)println 'Now: ${now}'println '1 Year from Now: ${oneYearFromNow}'
}

该演示表明原始的Date实例确实保持不变,并且提供了具有指定字段更改的副本。 GDK Calendar还有一个等效项,称为Calendar.updated(Map) 。

结论

我喜欢Groovy的一件事是SDK类的GDK扩展。 在本文中,我研究了JDK的Date的GDK Date扩展如何提供许多有用的便捷方法,从而使代码更简洁和可读性更好。

参考: Groovy JDK(GDK): JCG合作伙伴 Dustin Marx在“ 实际事件的启发”博客上的日期和日历 。

翻译自: https://www.javacodegeeks.com/2012/12/groovy-jdk-gdk-date-and-calendar.html

jdk groovy版本

jdk groovy版本_Groovy JDK(GDK):日期和日历相关推荐

  1. java怎么查看jdk版本_java版本和jdk版本必须一样

    1.首先需要检查你的系统是否安装了java环境,2.在"开始"==>"运行"==>输入"cmd"弹出命令窗口,在命令窗口中输入java或者javac ...

  2. Mac电脑jdk的安装和jdk版本的切换

    jdk版本的切换: 在mac切换jdk是一件非常容易的事情. 1,查看自己mac中的jdk版本,java -version是查看自己电脑当前正在使用的jdk版本,输入 /us r/libexec/ja ...

  3. java编译器jdk版本_以编程方式确定Java类的JDK编译版本

    java编译器jdk版本 当需要确定使用哪个JDK版本来编译特定的Java .class文件时, 通常使用的方法是使用javap并在javap输出中查找列出的"主要版本". 我在我 ...

  4. 以编程方式确定Java类的JDK编译版本

    当需要确定使用哪个JDK版本来编译特定的Java .class文件时, 通常使用的方法是使用javap并在javap输出中查找列出的"主要版本". 我在博客文章Autoboxing ...

  5. JDK各个版本的新特性jdk1.5-jdk8

    文章链接:JDK各个版本的新特性jdk1.5-jdk8 不过最有用的是java的lambda表达式,还有stream的运用,以及map和reduce.一篇文章就够了.厉害. jdk8新特性(部分) 接 ...

  6. 我应该使用哪个版本的 JDK?

    本文在写作过程中参考了whichJDK ​ 要构建和运行 Java 应用程序,就需要安装 JDK 环境. OpenJDK 是 Java SE 规范的开源软件,但它只是源代码.二进制发行版由不同的供应商 ...

  7. Oracle JDK高版本商用付费分析

    结论:JDK 8分情况 8u211和之后的版本商用需付费,JDK 9/10 免费 JDK 11及以上所有版本商用需付费 免费建议:JDK 8 使用8u202 版本,JDK8以上版本使用OpenJDK或 ...

  8. jdk各个版本及其代号

    jdk各个版本及其代号 表格如下: 版本号 名称 中文名 发布日期 JDK 1.1.4 Sparkler 宝石 1997-09-12 JDK 1.1.5 Pumpkin 南瓜 1997-12-13 J ...

  9. JDK各版本新特性(更新到Java20)

    Hello,I'm Shendi 每当Java出新版本我会第一时间更新 目录 各版本JDK下载 JDK 1.0 1996-01-23 Oak(橡树) JDK 1.1 1997-02-19 JDK 1. ...

最新文章

  1. Tensorflow实现神经网络及实现多层神经网络进行时装分类
  2. 【转载】SAP内部订单概念信息
  3. sqlmap —— os-shell参数分析
  4. windows下写代码在linux下编译,如何在Windows中编译Linux Unix的代码(采用cygwin)?...
  5. 项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)...
  6. 陈序猿,你敢创业吗?怎么才算成功?
  7. java aes mysql blob_使用带有ORDER子句的AES_DECRYPT在MySQL中返回BLOB数据
  8. Lesson 3 Part 1 Locally weighted regression
  9. Windows 的 80 端口被 System 进程占用解决方案
  10. 全网最简单解决OneNote中英字体不统一
  11. Java项目:医院门诊收费管理系统(java+html+jdbc+mysql)
  12. OEIS A098928 数表扩充
  13. Footprint:一夜暴涨250%的Boba,能否成为下个Layer 2代表
  14. 依赖计算机英语作文,过度依赖电脑的危害的英文作文
  15. 区块链学习2——区块链浏览器的搭建
  16. 天猫精灵智能设备对接(3)
  17. HyperLynx(二十三)DDR(六)DDRx总线批量仿真
  18. 外卖店优先级 第十届蓝桥杯真题 C++
  19. 基于Springboot的调查问卷管理系统
  20. 【FederatedLearning】联邦学习类别详述(横向、纵向、迁移)

热门文章

  1. python登录微信pc版_腾讯TIM iOS版2.5.6重大更新:新增支持微信帐号登录、语音进度条...
  2. 使用Qt合并图片的算法
  3. Android封装sdk页面为h5,Android/H5混合 SDK 集成文档
  4. 厦大 1395 组合
  5. winform设置透明图片
  6. python ----Parser使用
  7. H5 iframe标签的用法
  8. RS-485总线布线规范
  9. 判断数组相同数c语言_单片机常用的14个C语言算法,看过的都成了大神!
  10. excel解析html代码,使用excel vba解析HTML的错误