Using JODA to deal with the time(1)
Joda-time is much better than java.util.Calendar.
Joda Introduction
Joda-Time allows for multiple calendar systems. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included.
Supporting classes include time zone, duration, format and parsing.
Quick start guide
Date and Time
DateTime, DateMidnight, LocalDate, LocalTime, LocalDateTime
We can add the lib in sbt configuration file as follow:
organization := "com.sillycat"
name := "easyscala"
version := "1.0"
scalaVersion in ThisBuild := "2.10.0"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
resolvers ++= Seq(
"sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/",
"sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
"typesafe repo" at "http://repo.typesafe.com/typesafe/releases/",
"spray repo" at "http://repo.spray.io/",
"sbt-plugin-releases" at "http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/"
)
libraryDependencies ++= Seq(
"joda-time"% "joda-time"% "2.1",
"org.joda" % "joda-convert" % "1.3"
)
Pay attention to joda-convert, if I want to use the joda-time and constructor of DateTime.
I will get
Error Message:
org.joda.time.DateTime does not have a constructor
Solution:
Add the lib joda-convert and sbt update, sbt eclipse.
var dt = new DateTime(); //set the current date
All the datetime classes are immutable(like String). Simple methods have been provided to alter field values in a newly created object.
var year2000 = dt.withYear(2000) //setting 2000 year
var twoHoursLater = dt.plusHours(2) //setting 2 hours later
println("new DateTime Object = " + year2000)
println("new DateTime Object = " + twoHoursLater)
We can access all the fields of the date and time.
var monthName = dt.monthOfYear().getAsText() //String
var frenchShortName = dt.monthOfYear().getAsShortText(Locale.FRENCH) //String
var isLeapyear = dt.year().isLeap() //boolean
Intervals and time periods
Interval class holds a start and end date time, and allows operations based around that range of time.
Period class holds a period such as 6 months, 3 days and 7 hours. You can create a Period directly, or derive it from an interval.
Duration class holds an exact duration in milliseconds. It can be derive from an interval.
var dt1 = new DateTime(2005, 3, 26, 12, 0, 0 , 0)
//set the date and time to be 2005-03-26 T12:00:00.000
println(dt1)
var plusPeriod = dt.plus(Period.days(1))
// plus Period based on a date time to get a new date time
var plusDuration = dt.plus(Duration.millis(24L*60L*60L*1000L))
// plus Duration based on a date time to get to new date time
// and the unit of the time is milliseconds
User Guide
I may go in details after I solve my issue.
http://joda-time.sourceforge.net/userguide.html
References:
http://www.ibm.com/developerworks/cn/java/j-jodatime.html
http://joda-time.sourceforge.net/
http://joda-time.sourceforge.net/api-release/index.html
http://joda-time.sourceforge.net/userguide.html
http://www.tutorialspoint.com/scala/scala_data_types.htm
分享到:
相关推荐
赠送jar包:joda-time-2.8.1.jar 赠送原API文档:joda-time-2.8.1-javadoc.jar 赠送源代码:joda-time-2.8.1-sources.jar 包含翻译后的API文档:joda-time-2.8.1-javadoc-API文档-中文(简体)版.zip 对应Maven信息...
赠送jar包:joda-time-2.10.8.jar; 赠送原API文档:joda-time-2.10.8-javadoc.jar; 赠送源代码:joda-time-2.10.8-sources.jar; 赠送Maven依赖信息文件:joda-time-2.10.8.pom; 包含翻译后的API文档:joda-time-...
joda-time-2.1.jar
**Joda-Time:Java日期和时间处理的利器** Joda-Time是Java开发中的一款非常流行的开源库,专门用于处理日期和时间。它弥补了Java标准库`java.util.Date`和`Calendar`在处理时间问题上的不足,提供了更加强大、灵活...
赠送jar包:joda-time-2.10.4.jar; 赠送原API文档:joda-time-2.10.4-javadoc.jar; 赠送源代码:joda-time-2.10.4-sources.jar; 赠送Maven依赖信息文件:joda-time-2.10.4.pom; 包含翻译后的API文档:joda-time-...
《Joda-Time 2.9.9:时间日期处理的高效工具》 Joda-Time是Java平台上一个著名的日期和时间处理库,它弥补了Java标准库中日期时间处理的不足。在2.9.9这个版本中,我们看到了对日期时间操作的强大支持和丰富的功能...
1. **更易用的API**:Joda-Time的API设计得更加直观,使得代码更简洁、易读。 2. **更好的类型安全**:通过使用枚举类型表示月份、日历字段等,减少了潜在的错误。 3. **时区处理**:提供了对世界各地时区的全面支持...
赠送jar包:joda-time-2.9.9.jar; 赠送原API文档:joda-time-2.9.9-javadoc.jar; 赠送源代码:joda-time-2.9.9-sources.jar; 赠送Maven依赖信息文件:joda-time-2.9.9.pom; 包含翻译后的API文档:joda-time-...
1. **格式化与解析**:Joda-Time提供DateTimeFormatter,可以自定义日期时间格式进行解析和格式化,避免了使用SimpleDateFormat带来的线程安全问题。 2. **时间区间操作**:通过Interval类,可以方便地进行时间区间...
1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...
赠送jar包:joda-time-2.2.jar; 赠送原API文档:joda-time-2.2-javadoc.jar; 赠送源代码:joda-time-2.2-sources.jar; 赠送Maven依赖信息文件:joda-time-2.2.pom; 包含翻译后的API文档:joda-time-2.2-javadoc-...
1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...
赠送jar包:joda-time-2.9.5.jar; 赠送原API文档:joda-time-2.9.5-javadoc.jar; 赠送源代码:joda-time-2.9.5-sources.jar; 赠送Maven依赖信息文件:joda-time-2.9.5.pom; 包含翻译后的API文档:joda-time-...
Joda-Time 令时间和日期值变得易于管理、操作和理解。日期和时间是一个非常复杂的领域。Joda-Time中的许多类旨在使域的细微差别得以充分表达。版本2.10.6是当前的最新版本。该版本被认为是稳定的,值得2.x标签使用。
Joda-Time是一个强大、易用、高效的日期和时间库,可以利用它把JDK Date和Calendar类完全替换掉无论是计算日期,打印日期、或者解析日期,都非常使用,便捷.附件中有最新的jar包,然后有对JodaTime详细的解释,以及...
赠送jar包:joda-time-2.5.jar; 赠送原API文档:joda-time-2.5-javadoc.jar; 赠送源代码:joda-time-2.5-sources.jar; 赠送Maven依赖信息文件:joda-time-2.5.pom; 包含翻译后的API文档:joda-time-2.5-javadoc-...
joda-time-2.7-javadoc.jar。 Joda-Time令时间和日期值变得易于管理、操作和理解。并且 Joda 与 JDK 的互操作性也方便程序使用。目前Joda Time 已经纳入 JDK 8 的官方API了。
(JDK1.8的API已经重写了日期库,引入的java.time包,其实也是借鉴了Joda-Time) --------------------- 作者:--nodeps 来源:CSDN 原文:https://blog.csdn.net/backbug/article/details/78646152 版权声明:...