-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimestampUtils.java
83 lines (61 loc) · 2.68 KB
/
TimestampUtils.java
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
75
76
77
78
79
80
81
82
83
public static String getDate(long timestamp, String format) {
SimpleDateFormat sfd = new SimpleDateFormat(format);
return sfd.format(new Date(timestamp));
}
public static String getDate(long timestamp) {
SimpleDateFormat sfd = new SimpleDateFormat("MMM dd, yyyy");
return sfd.format(new Date(timestamp));
}
public static String parseDate(String inputDateString, SimpleDateFormat inputDateFormat, SimpleDateFormat outputDateFormat) {
Date date = null;
String outputDateString = null;
try {
date = inputDateFormat.parse(inputDateString);
outputDateString = outputDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return outputDateString;
}
public static String getLongDate(String date) {
String formatedDate = date;
SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat EEEddMMMyyyy = new SimpleDateFormat("dd MMM, yyyy");
formatedDate = parseDate(date, ymdFormat, EEEddMMMyyyy);
return formatedDate;
}
public static String getDateFormat(String date, boolean isReverse) {
String formatedDate = date;
SimpleDateFormat ymdFormat = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat EEEddMMMyyyy = new SimpleDateFormat("yyyy-MM-dd");
if (isReverse) {
formatedDate = parseDate(date, EEEddMMMyyyy, ymdFormat);
} else {
formatedDate = parseDate(date, ymdFormat, EEEddMMMyyyy);
}
return formatedDate;
}
public static String getTimeAgoDate(long pastTimeStamp) {
// for 2 min ago use DateUtils.MINUTE_IN_MILLIS
// for 2 sec ago use DateUtils.SECOND_IN_MILLIS
// for 1 hours ago use DateUtils.HOUR_IN_MILLIS
long now = System.currentTimeMillis();
if (now - pastTimeStamp < 1000) {
pastTimeStamp = pastTimeStamp + 1000;
}
CharSequence ago =
DateUtils.getRelativeTimeSpanString(pastTimeStamp, now, DateUtils.SECOND_IN_MILLIS);
return ago.toString();
}
public static Boolean isSameDay(long mPreviousTimestamp, long latestTimestamp) {
boolean isSameDay = false;
Date d = new Date(mPreviousTimestamp);
Date d2 = new Date(latestTimestamp);
Calendar c = Calendar.getInstance();
c.setTime(d);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
isSameDay = c.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
return isSameDay;
}