【轉】scala 時間、時間戳系列操作
格式說明:
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
格式示例:
Date and Time Pattern Result
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" Wed, Jul 4, '01
"h:mm a" 12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" 010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u" 2001-W27-3
"自由輸入YYYY-'愛你哦'ww-u自由輸入" 自由輸入2001-愛你哦27-3自由輸入
import java.text.SimpleDateFormat import java.util.{Calendar, Date} /** * 獲取當前時間 * * @param pattern pattern 如"yyyyMMddHHmmss" * @return */ def getrealTime(pattern: String): String = { val timeTag = System.currentTimeMillis() val changeTime = new Date(timeTag) val dataFormat = new SimpleDateFormat(pattern) dataFormat.format(changeTime) } /** * 獲取當前時間戳(精確到毫秒) * * @return */ def getTimestamp(): Long = { val time = Util.getrealTime("yyyyMMddHHmmss") Util.funStringToTimeStamp(time, "yyyyMMddHHmmss") } /** * 將時間字串修改為時間戳 * * @param time 時間 * @param timeFormatted 時間格式 如 "yyyyMMddHHmmss" * @return 精確到毫秒的時間戳 */ def funStringToTimeStamp(time: String, timeFormatted: String): Long = { val fm = new SimpleDateFormat(timeFormatted) val dt = fm.parse(time) dt.getTime } /** * 將時間戳轉換為時間 * * @param timestamp 精確到毫秒 * @param timeFormatted 時間格式如“HH”表示按24小時制返回時間戳對應的小時 * @return */ def timestampToString(timestamp: String, timeFormatted: String): String = { val fm = new SimpleDateFormat(timeFormatted) val time = fm.format(new Date(timestamp.toLong)) time } /** * 獲取傳入日期與當前日期的天數差 * * @param day 待比較日期(僅包含月日) * @param dayFormatted 待比較日期格式(例如:MMdd或MM-dd) * @return */ def daysBetweenToday(day: String, dayFormatted: String): Int = { val calendar = Calendar.getInstance calendar.setTime(new Date()) val today = calendar.get(Calendar.DAY_OF_YEAR) val year = calendar.get(Calendar.YEAR) val inputF = new SimpleDateFormat("yyyy" + dayFormatted) val date = inputF.parse(year + day) calendar.setTime(date) val inputDay = calendar.get(Calendar.DAY_OF_YEAR) var days = today - inputDay if (days < 0) { val beforeYearDate = inputF.parse((year - 1) + day) calendar.setTime(beforeYearDate) days = calendar.getActualMaximum(Calendar.DAY_OF_YEAR) - calendar.get(Calendar.DAY_OF_YEAR) + today } return days } /** * 將毫秒級時間戳轉化成分鐘級時間戳 * * @param time 毫秒級時間戳 * @return 分鐘級時間戳 */ def getMinTimestamp(time: Long): Long = { val minTime = time / (1000 * 60) minTime } /** * 將時間字串修改為格式 * * @param inpuTime 輸入時間 * @param inputFormatted 輸入時間格式 * @param outputFormatted 輸出時間格式 * @return */ def formatTime(inpuTime: String, inputFormatted: String, outputFormatted: String): String = { val inputF = new SimpleDateFormat(inputFormatted) val outputF = new SimpleDateFormat(outputFormatted) val inputT = inputF.parse(inpuTime) outputF.format(inputT) } /** * 獲取傳入時間戳的天數差 * * @param t1 較小時間戳 * @param t2 較大時間戳 * @return */ def caculate2Days(t1: Long, t2: Long): Int = { import java.util.Calendar val calendar = Calendar.getInstance calendar.setTimeInMillis(t2) val t2Day = calendar.get(Calendar.DAY_OF_YEAR) calendar.setTimeInMillis(t1) val t1Day = calendar.get(Calendar.DAY_OF_YEAR) var days = t2Day - t1Day if (days < 0) { days = calendar.getActualMaximum(Calendar.DAY_OF_YEAR) - t1Day + t2Day } return days; } /** * 判斷nowTime是否在startTime與endTime之間 * @param nowTime * @param startTime * @param endTime * @param formater * @return */ def isBetweenDate(nowTime: String, startTime: String, endTime: String, formater: String): Boolean = { val df = new SimpleDateFormat(formater) val nowDate = df.parse(nowTime) val startDate = df.parse(startTime) val endDate = df.parse(endTime) if ((nowDate.getTime == startDate.getTime) || (nowDate.getTime == endDate.getTime)) return true if (nowDate.after(startDate) && nowDate.before(endDate)) { true } else { false } } /** * 時間增加天數,返回時間 * * @param date 入參時間 * @param num 增加的天數 * @return */ def funAddDate(date: String, num: Int): String = { val myformat = new SimpleDateFormat("yyyyMMdd") var dnow = new Date() dnow = myformat.parse(date) val cal = Calendar.getInstance() cal.setTime(dnow) cal.add(Calendar.DAY_OF_MONTH, num) val newday = cal.getTime myformat.format(newday) } /** * 時間增加小時,返回時間 * * @param date 入參時間 * @param num 增加的天數 * @return */ def funAddHour(date: String, num: Int): String = { val myformat = new SimpleDateFormat("yyyyMMddHH") var dnow = new Date() dnow = myformat.parse(date) val cal = Calendar.getInstance() cal.setTime(dnow) cal.add(Calendar.HOUR, num) val newday = cal.getTime myformat.format(newday) } /** * 時間增加分鐘,返回時間 * * @param date 入參時間 * @param num 增加的分鐘數 * @return */ def funAddMinute(date: String, num: Int): String = { val myformat = new SimpleDateFormat("yyyyMMddHHmm") var dnow = new Date() dnow = myformat.parse(date) val cal = Calendar.getInstance() cal.setTime(dnow) cal.add(Calendar.MINUTE, num) val newday = cal.getTime myformat.format(newday) } /** * 時間增加秒,返回時間 * * @param date 入參時間 * @param num 增加的秒數 * @return */ def funAddSecond(date: String, num: Int, format: String): String = { val myformat = new SimpleDateFormat(format) var dnow = new Date() dnow = myformat.parse(date) val cal = Calendar.getInstance() cal.setTime(dnow) cal.add(Calendar.SECOND, num) val newday = cal.getTime myformat.format(newday) } /** 獲取過去幾天的時間陣列 * * @param date * @param num * @return */ def getPastDays(date: String, num: Int): Array[String] = { val buffer = new ArrayBuffer[String]() val range = 0 until num for(i <- range){ buffer.append(funAddDate(date,-i)) } buffer.toArray } /** 獲取過去幾小時的時間陣列 * * @param date * @param num * @return */ def getPastHours(date: String, num: Int, interval:Int): Array[String] = { val buffer = new ArrayBuffer[String]() val range = 0 until num for(i <- range){ buffer.append(funAddHour(date,-i*interval)) } buffer.toArray }