-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateUtils.java
162 lines (132 loc) · 5.48 KB
/
DateUtils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import android.icu.util.GregorianCalendar;
import android.util.Log;
import org.joda.time.DateTime;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
* Created by Hasan Mhd Amin on 9/12/2018.
* <p>
* required libraries to install:
* * joda-time-android: https://github.com/dlew/joda-time-android#usage
*/
public class DateUtils {
private static final String TAG = "DateUtils";
public static long ONE_DAY_IN_MILLISECOND = 86400000;
static SimpleDateFormat sdf = null;
public static String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month - 1];
}
public static String convertDateToString(Date date, String format) {
//if(sdf==null) {
sdf = new SimpleDateFormat(format);
// sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//}
//sdf.applyPattern(format);
return sdf.format(date);
}
public static Date convertStringToDate(String date, String format) {
if (sdf == null) {
sdf = new SimpleDateFormat();
}
sdf.applyPattern(format);
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String changeDateStringFormat(String date, String oldFormat, String newFormat) {
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
cal.setTime(sdf.parse(date));
Date calendar = cal.getTime();
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat, Locale.US);
Log.e("sdf", sdf2.format(calendar.getTime()));
return sdf2.format(calendar.getTime());
} catch (Exception e) {
e.printStackTrace();
return oldFormat;
}
}
public static DateTime calendarToTime(Calendar calendar) {
DateTime date = new DateTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
return date;
}
public static ArrayList<Date> generateDaysArray(Calendar startDay, Calendar endDay) {
ArrayList<Date> dateList = new ArrayList<>();
while (startDay.compareTo(endDay) < 1) {
dateList.add(startDay.getTime());
startDay.add(Calendar.DAY_OF_YEAR, 1);
}
return dateList;
}
public static Calendar timestampToCalender(String timestamp) {
long timeStamp = Long.parseLong(timestamp);
return timestampToCalender(timeStamp);
}
public static Calendar timestampToCalender(long timestamp) {
//TODO: check for timezone, leap month, etc. in manual conversion.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
return calendar;
}
/**
* Gets the timestamp of the start day.
* for example: the timestamp of 26/09/1993 14:07
* and the output will be the timestamp of 26/09/1993 00:00
*
* @param timeInMillis the timestamp of a given date
* @return the timestamp of the start of the day.
*/
public static long startOfDayTimestamp(long timeInMillis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.set(Calendar.MILLISECOND,0);
cal.set(Calendar.HOUR_OF_DAY, 0); //set hours to zero
cal.set(Calendar.MINUTE, 0); // set minutes to zero
cal.set(Calendar.SECOND, 0); //set seconds to zero
cal.set(Calendar.MILLISECOND, 0); //set milliseconds to zero
return cal.getTimeInMillis();
}
public static long startOfDayLocalTimestamp(long timeInMillis) {
return convertUtcTimestampToLocalTime(startOfDayTimestamp(timeInMillis));
}
public static String splitToComponentTimes(float longVal) {
int hours = (int) longVal / 3600;
int remainder = (int) longVal - hours * 3600;
int mins = remainder / 60;
remainder = remainder - mins * 60;
int secs = remainder;
// int[] ints = {hours , mins , secs};
String result = hours + ":" + mins + ":" + secs;
return result;
}
public static Long getCurrentTimeMillis(){
return System.currentTimeMillis();
}
public static Long getLocalCurrentTimeMillis(){
long s = System.currentTimeMillis();
int offset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
return s + offset;
}
public static Long convertUtcTimestampToLocalTime(long UtcTimestamp){
int offset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
return UtcTimestamp + offset;
}
public static String convertTimestampToReadableDuration(long timestampDuration){
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(timestampDuration),
TimeUnit.MILLISECONDS.toMinutes(timestampDuration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timestampDuration)),
TimeUnit.MILLISECONDS.toSeconds(timestampDuration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timestampDuration)));
return hms;
}
}