date.iterator.count.util.DateUtil.scala Maven / Gradle / Ivy
package date.iterator.count.util
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.Date
object DateUtil {
private val dateUtil = new DateUtil
def abstractBirthday(identity : String, birthday : Timestamp): Timestamp = dateUtil.abstractBirthday(identity, birthday)
def abstractBirth(identity : String, birthday : Timestamp): String = dateUtil.abstractBirth(identity, birthday)
def toDate(day : String, pattern : String): Timestamp = dateUtil.toDate(day, pattern)
def toDate(day : String): Timestamp = dateUtil.toDate(day, "yyyyMMdd")
def toDay(day : Timestamp) : Date = dateUtil.toDate(day)
def getBySplit(target : String, count : Int): String ={
var c = count
var i : Int = 0
while (c > 0) {
val index = target.indexOf(",",i)+1
if ( index <= 0) {
return target
}
i = index
c=c-1
}
target.substring(0,i-1)
}
def checkInterval(value : Long): String = {
var result = ""
val second = Math.ceil(value / 1000)
var day = 0.0
if (second > 60*60*60) {
day = Math.floor(second / 60*60*60)
result += day + "天"
}
val l = second - day * 60*60*60
var hour = l
if (hour > 60 * 60) {
hour = Math.floor(hour / 60*60)
result += hour + "小时"
}
var c = l - hour * 60*60
if (c > 60) {
c = Math.floor(c / 60)
result += c + "分钟"
}
result
}
def main(args: Array[String]): Unit = {
println(checkInterval(1273075200000L))
// val aaa = "a,b,c,d,e"
// val c = 3
// println(getBySplit(aaa,c))
/*var i : Int = 0
while (c > 0) {
val index = aaa.indexOf(",",i)+1
if ( index <= 0) {
i = aaa.length+1
} else {
i = index
}
c=c-1
println(c)
}
println(aaa.substring(0,i-1))*/
/*var i : Int = 1
while (i < 13) {
var a = 10
while (a < 31) {
println(i + ":" + a + ":" + dateUtil.getZodiac(i, a))
a += 10
}
i += 1
}*/
}
}
class DateUtil {
def abstractBirth(identity : String, birthday : Timestamp): String = {
val day = abstractDate(identity)
if (day != null) return day
toString(birthday, "yyyyMMdd")
}
private def abstractBirthday(identity : String, birthday : Timestamp): Timestamp = {
val day = abstractDate(identity)
if (day == null) return birthday
val result = toDate(day, "yyyyMMdd")
if (result == null) {
return birthday
}
result
}
private def toString(birthday : Timestamp, pattern : String): String = {
if (birthday == null) return null
new SimpleDateFormat(pattern)
.format(birthday.getTime)
}
private def abstractDate(identity : String): String ={
val day =
if (identity.length == 15) {
"19" + identity.substring(6, 12)
}
else if (identity.length == 18) {
identity.substring(6, 14)
} else {
null
}
day
}
private def toDate(day : String, pattern : String): Timestamp = {
try {
import java.util.Locale
val loc = new Locale("en")
val date = new SimpleDateFormat(pattern, loc)
date.setLenient(false) // 1150 not convert to 1220
new Timestamp(date.parse(day).getTime)
} catch {
case ex: Throwable =>{
// println("day : " + day + " : " + ex.getMessage)
null
}
}
}
private def toDate(value:Timestamp) : Date= {
new Date(value.getTime)
}
}