-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeUtils.java
More file actions
75 lines (60 loc) · 2.07 KB
/
DateTimeUtils.java
File metadata and controls
75 lines (60 loc) · 2.07 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
75
package common.android.fiot.androidcommon;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Created by caoxuanphong on 4/29/16.
*/
public class DateTimeUtils {
private static final String TAG = "DateTimeUtils";
public static long currentTimeToEpoch() {
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
Log.i(TAG, "currentTimeToEpoch: " + offsetFromUtc + ", " + System.currentTimeMillis());
return (System.currentTimeMillis() / 1000 + offsetFromUtc);
}
/**
* Get current timezone of device
* @return
*/
public static int getTimezone() {
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
int offsetFromUtc = tz.getOffset(now.getTime());
return offsetFromUtc/3600000;
}
public static String epochToString(long epoch, String format) {
//Date date = new Date((epoch - offsetFromUtc) * 1000);
Date date = new Date((epoch) * 1000);
SimpleDateFormat s = new SimpleDateFormat(format);
return s.format(date);
}
public static Date epochToDate(long epoch) {
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
int offsetFromUtc = tz.getOffset(now.getTime()) / 1000;
Date date = new Date((epoch - offsetFromUtc) * 1000);
return date;
}
public static boolean isSameDate(long epoch1, long epoch2) {
Date date1 = epochToDate(epoch1);
Date date2 = epochToDate(epoch2);
if (date1.getDate() == date2.getDate() &&
date1.getMonth() == date2.getMonth() &&
date1.getYear() == date2.getYear()) {
return true;
}
return false;
}
/**
* Get current time
* @param format Wanted format
* @return
*/
public static String getCurrentTime(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
}