Using JODA to deal with the time(1)

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


你可能感兴趣的:(time)