-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdateFunctions.scala
More file actions
74 lines (61 loc) · 2.2 KB
/
dateFunctions.scala
File metadata and controls
74 lines (61 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package common.utility
import java.text.SimpleDateFormat
import java.util.Date
/**
* Created by markmo on 27/02/2016.
*/
object dateFunctions {
val OUTPUT_DATE_PATTERN = "yyyy-MM-dd"
val OUTPUT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"
/**
* Converts a date string of a given pattern to a Date object.
*
* @param str String date to convert
* @param pattern String format of date string to parse
* @return Date
*/
def convertStringToDate(str: String, pattern: String) = {
val format = new SimpleDateFormat(pattern)
format.parse(str)
}
/**
* Formats a date string of a given pattern to a conformed format (yyyy-MM-dd).
*
* @param str String date to format
* @param pattern String format of date string to parse
* @return String formatted date (yyyy-MM-dd)
*/
def formatDateString(str: String, pattern: String) = {
val dt = convertStringToDate(str, pattern)
val outputFormat = new SimpleDateFormat(OUTPUT_DATE_PATTERN)
outputFormat.format(dt)
}
/**
* Formats a date string of a given pattern to a conformed date and time format (yyyy-MM-dd HH:mm:ss).
*
* @param str String date to format
* @param pattern String format of date string to parse
* @return String formatted date and time (yyyy-MM-dd HH:mm:ss)
*/
def formatDateTimeString(str: String, pattern: String) = {
val dt = convertStringToDate(str, pattern)
val outputFormat = new SimpleDateFormat(OUTPUT_DATE_TIME_PATTERN)
outputFormat.format(dt)
}
/**
* Converts a date string of a given pattern to epoch (unix) time.
*
* Defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.
*
* @param str String date to parse
* @param pattern String format of date string to parse
* @return long epoch (unix) time
*/
def convertStringToTimestamp(str: String, pattern: String) = {
convertStringToDate(str, pattern).getTime
}
implicit class RichDate(val date: Date) extends AnyVal {
def <=(when: Date): Boolean = date.before(when) || date.equals(when)
def >=(when: Date): Boolean = date.after(when) || date.equals(when)
}
}