summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/utils/DateHelper.java
blob: 2e9c2b24f7a0da3760f133f1f7e6f7b578f5a2ed (plain)
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
package se.leap.bitmaskclient.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Contains helper methods related to date manipulation.
 *
 * @author Janak
 */
public class DateHelper {
    private static final String DATE_PATTERN = "dd/MM/yyyy";
    private static final String DATE_PATTERN_RFC822_NUMERIC_ZONE = "dd MMM yy hh:mm Z"; // RFC822 with numeric zone
    private static final int ONE_DAY = 86400000; //1000*60*60*24

    public static long getDateDiffToCurrentDateInDays(String startDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
        Date lastDate = sdf.parse(startDate);
        Date currentDate = new Date();
        return (currentDate.getTime() - lastDate.getTime()) / ONE_DAY;
    }

    public static String getCurrentDateString() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
        Date lastDate = new Date();
        return sdf.format(lastDate);
    }

    /**
     * Return date in a specific format bitmaskcore can handle.
     * @param milliSeconds Date in milliseconds
     * @return String representing date in specified format
     */
    public static String getFormattedDateWithTimezone(long milliSeconds) throws IllegalArgumentException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_RFC822_NUMERIC_ZONE, Locale.US);
        return sdf.format(milliSeconds);
    }
}