-
Notifications
You must be signed in to change notification settings - Fork 3
/
DateUtil.java
103 lines (88 loc) · 3.02 KB
/
DateUtil.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
package com.android.kickstart.utils;
import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* @return : Returns the current time without rounding.
*/
public static Date getCurrentDate() {
return new Date();
}
/**
* @param format : Date format that you want
* @return : Returns the current date
*/
public static String getCurrentDate(String format) {
return dateToString(new Date(), format);
}
/**
* @param when : Date
* @return : Given a date, return the previous date of the given date (24 hrs before).
*/
public static Date previousDate(Date when) {
long time = when.getTime() - (24 * 60 * 60 * 1000);
return new Date(time);
}
/**
* Given a date, return the next date of the given date (24 hrs after).
*/
/**
* @param when : Date
* @return : Given a date, return the next date of the given date (24 hrs after).
*/
public static Date nextDate(Date when) {
long time = when.getTime() + (24 * 60 * 60 * 1000);
return new Date(time);
}
/**
* Given a date, return the previous date of the given days.
*/
public static Date previousDate(Date when, int days) {
long time = when.getTime() - (days * (24 * 60 * 60 * 1000));
return new Date(time);
}
/**
* @param when : Date
* @param days : Number of days
* @return : Given a date, return the next date of the given days.
*/
public static Date nextDate(Date when, int days) {
long time = when.getTime() + (days * (24 * 60 * 60 * 1000));
return new Date(time);
}
/**
* @param dateString : Date in string
* @param format : format of dateString
* @return : Date
* @throws ParseException : Exception
*/
public static java.util.Date stringToDate(String dateString, String format) throws ParseException {
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setLenient(false);
return simpleDateFormat.parse(dateString);
}
/**
* @param date : Date
* @param format : Format of Date
* @return : dateString
*/
public static String dateToString(java.util.Date date, String format) {
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
/**
* @param year : Year
* @param month : Month
* @return : Return number of days of given year and month
*/
public static int getDaysOfMonth(int year, int month) {
if (String.valueOf(year).length() > 4 || month > 12 || month < 0)
return -1;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}