[Scala] joda DateTime vs java util Date

I - Setup

*) all example in this tutorial write on base play framework 2.4 and java version "1.8.0_65"
To use joda DateTime you need to import :
import org.joda.time.DateTime
To use java util Date you need to import :
import java.util.Date

II - Creat new

Create new with current time :
def index = Action {
//with joda DateTime
val currentTimeWithJoda = new DateTime()
// return: 2016-01-03T17:00:20.358+07:00

//with java Date
val currentTimeWithJavaDate = new Date()
// return: Sun Jan 03 17:00:20 ICT 2016

Ok("OK")
}
Create with specific date :
 both have same format to create (year : Int, month : Int, day : Int, hrs : Int, min : Int, sec : Int)
def index = Action {
//with joda DateTime
val currentTimeWithJoda = new DateTime(2016,1,30,0,0,0)
//return 2016-01-30T00:00:00.000+07:00

//with java Date
val currentTimeWithJavaDate = new Date(2016,1,30,0,0,0)
//return Wed Mar 01 00:00:00 ICT 3916

Ok("OK")
}
 Create with TimeZone :
 You can get timeZone id from here : https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
 With joda DateTime you need import :
import org.joda.time.DateTimeZone
//with joda DateTime
val currentWithVNTimeZone = new DateTime(DateTimeZone.forID("Asia/Ho_Chi_Minh"))
//return : 2016-01-03T18:32:57.774+07:00

val timeWithVNTimeZone = new DateTime(2016,1,30,0,0,0,DateTimeZone.forID("Asia/Ho_Chi_Minh"))
// return: 2016-01-30T00:00:00.000+07:00

val timeWithJPTimeZone = new DateTime(2016,1,30,0,0,0,DateTimeZone.forID("Asia/Tokyo"))
// return: 2016-01-30T00:00:00.000+09:00

 With java util Date quite hard , you could also set the timezone for all.If you have better way please tell me.First with java Date you need import :
import java.util.TimeZone
//with java Date
val currentTimeWithJavaDateBeforeSet = new Date()
//return: Sun Jan 03 11:38:43 GMT 2016

TimeZone.setDefault(TimeZone.getTimeZone("ICT"));

val currentTimeWithJavaDateAfterSet = new Date()
//return: Sun Jan 03 11:38:43 GMT 2016 ??? same above

III -Convert to another type

1. Switch between joda DateTime and java Date:
def index = Action {
//with joda DateTime
val currentTimeWithJoda = new DateTime()
//return: 2016-01-03T17:00:20.358+07:00
val convertToJavaDate = currentTimeWithJoda.toDate
//return: Sun Jan 03 17:00:20 ICT 2016

//with java Date
val currentTimeWithJavaDate = new Date()
//return: Sun Jan 03 17:00:20 ICT 2016
val convertToJodaDateTime = new DateTime(currentTimeWithJavaDate)
//return: 2016-01-03T17:00:20.358+07:00

Ok("OK")
}
2. Convert to String :
//with joda DateTime
val currentTimeWithJoda = new DateTime()
//return : 2016-01-03T19:30:26.740+07:00
val jodaToString = currentTimeWithJoda.toString()
//return : "2016-01-03T19:30:26.740+07:00"
val jodaToStringFormat = currentTimeWithJoda.toString("yyyy-MM-dd")
//return : "2016-01-03"
*)With java Date you need import:
import java.text.SimpleDateFormat
//with java Date
val currentTimeWithJavaDate = new Date()
//return : Sun Jan 03 12:30:26 GMT 2016
val javaDateToString = currentTimeWithJavaDate.toString()
//return : "Sun Jan 03 12:30:26 GMT 2016"
val formatter = new SimpleDateFormat("yyyy-MM-dd");
val javaDateToStringFormat = formatter.format(currentTimeWithJavaDate)
//return : "2016-01-03"
Some simple format
yyyy-MM-dd                 results in    2016-01-03
yyyyMMdd results in 20160103
EEE MMM dd hh:mm:ss yyyy results in Sun Jan 03 07:53:04 2016

IV - Create from String

1. java.util.Date
val formatter = new SimpleDateFormat("yyyy/MM/dd");
val stringFormatToJavaDate = formatter.parse("2017/01/30")
//return : Mon Jan 30 00:00:00 GMT 2017
*) string must match with format is defined

2. joda DateTime
By default
val currentTimeWithJoda = new DateTime("2017-01-30")
//return : 2017-01-30T00:00:00.000+07:00
With dynamic format :
import org.joda.time.format.DateTimeFormat
val fromDynamicString = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss")
.parseDateTime("30/01/2017 20:27:05")
//return : 2017-01-30T20:27:05.000+07:00
*)you must defined format match with string first
. . .
Just all for today,leave your comment

Comments