/*
 *  @(#) DateTimeUtils.java v1.0 2011/10/28 10:49:21 AM
 *
 *  Copyright 2014 Viettel ICT. All rights reserved.
 *  VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author Huynt74
 */
public class DateTimeUtils {

    /**
     * getCurrentDateString
     *
     * @param dateFormat
     * @return
     */
    public static String getDateString(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);
    }

    /**
     * Current date string in format yyyyMMddHHmmss
     *
     * @return
     */
    public static String getCurrentDateLongString() {
        return getDateString(Calendar.getInstance().getTime(), "MMddHHmmss");
    }

    /**
     * Current date string in format yyyyMMddHHmmss
     *
     * @return
     */
    public static String getCurrentDateFullString() {
        return getDateString(Calendar.getInstance().getTime(), "yyyyMMddHHmmss");
    }

    /**
     * Current date string in format dd/MM/yyyy HH:mm:ss
     *
     * @return
     */
    public static String getCurrentDateFullString2() {
        return getDateString(Calendar.getInstance().getTime(), "dd/MM/yyyy HH:mm:ss");
    }

    /**
     * Current date string in format MMdd
     *
     * @return
     */
    public static String getCurrentDateShortString() {
        return getDateString(Calendar.getInstance().getTime(), "yyMMdd");
    }

    /**
     * Get string date yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getCurrentDateByTimeStamp() {
        return getDateString(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Get string date yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getCurrentDate() {
        return getDateString(Calendar.getInstance().getTime(), "dd/MM/yyyy");
    }

    /**
     * Convert string yyyyMMddHHmmss to timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp convertStringToTimeStamp(String date) {
        Date time = convertStringToTime(date, "yyyyMMddHHmmss");
        if (time == null) {
            return null;
        }
        return new Timestamp(time.getTime());
    }

    /**
     * Convert string yyyy-MM-dd HH:mm:ss to timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp convertStringToTimeStamp2(String date) {
        Date time = convertStringToTime(date, "yyyy-MM-dd HH:mm:ss");
        if (time == null) {
            return null;
        }
        return new Timestamp(time.getTime());
    }
    /**
     * Convert string yyyy-MM-dd HH:mm:ss to timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp convertStringToTimeStamp3(String date) {
        Date time = convertStringToTime(date, "yyyy-MM-dd");
        if (time == null) {
            return null;
        }
        return new Timestamp(time.getTime());
    }

    /**
     * Get string from date yyyy-MM-dd MM:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateByTimeStamp(Date date) {
        return getDateString(date, "yyyy-MM-dd HH:mm:ss");
    }
    /**
     * Get string from date yyyy-MM-dd MM:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateByddMMYYYHHmmss(Date date) {
        return getDateString(date, "dd-MM-yyyy HH:mm:ss");
    }

    /**
     * Get date object with string date and its format
     *
     * @param dateString
     * @param dateFormat
     * @return~
     * @throws java.text.ParseException
     */
    public static Date getDateString(String dateString, String dateFormat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.parse(dateString);
    }

    /**
     * Convert string to time
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date convertStringToTime(String date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(date);

        } catch (ParseException e) {
            System.out.println("Date ParseException, string value:" + date);
        }
        return null;
    }

    /**
     * convert dateTime to string
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String convertDateTimeToString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * Check valid phone number
     *
     * @param mobile
     * @return
     */
    public static boolean isValidMobile(String mobile) {
        return mobile != null && isAllNumber(mobile) && mobile.length() >= 9 && mobile.length() < 13;
    }

    public static boolean isAllNumber(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

    public static String padleft(String s, int len, char c) {
        s = s.trim();
        if (s.length() > len) {
            s = s.substring(s.length() - len, s.length());
        }
        StringBuilder d = new StringBuilder(len);
        int fill = len - s.length();
        while (fill-- > 0) {
            d.append(c);
        }
        d.append(s);
        return d.toString();
    }

    /**
     * random number
     *
     * @param max
     * @return
     */
    public static String randomNumber(int max) {
        Random randomGenerator = new Random();
        return String.valueOf(randomGenerator.nextInt(max));
    }

    /**
     * Current date string in format yyMMdd
     *
     * @return
     */
    public static String getCurrentDateString() {
        return getDateString(Calendar.getInstance().getTime(), "yyMMddHHmmss");
    }

    /**
     * Convert string dd/mm/yyyy to timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp convertToTimeStamp(String date) {
        Date time = convertStringToTime(date, "dd/MM/yyyy");
        if (time == null) {
            return null;
        }
        return new Timestamp(time.getTime());
    }

    /**
     * Current date string in format yyyyMMddHHmmss
     *
     * @return
     */
    public static String getCurrentDateFullString(Timestamp timestamp) {

        Date date = new Date(timestamp.getTime());
        return getDateString(date, "yyyyMMddHHmmss");
    }

    /**
     * convert from date string: yyyy-MM-dd HH:mm:ss.SS to date string
     * dd/MM/yyyy
     *
     * @return date string: dd/MM/yyy
     */
    public static String convertDateStringOther(String dateString) throws Exception {

        Date date = getDateString(dateString, "yyyy-MM-dd HH:mm:ss.SS");
        String dateStr = getDateString(date, "dd/MM/yyyy");
        return dateStr;
    }

    public static long subtractDateTimeStringToDay(String dateTimeA, String dateTimeB, String pattern) {
        long dateA = DateTimeUtils.isDateTimeString(dateTimeA, pattern);
        long dateB = DateTimeUtils.isDateTimeString(dateTimeB, pattern);
        if (dateA == -1 || dateB == -1) {
            return -1;
        }
        if (dateA < dateB) {
            return -1;
        }
        return Math.abs((dateA - dateB) / (1000 * 60 * 60 * 24));
        
    }

    public static long subtractDateTimeStringToMonth(String dateTimeA, String dateTimeB, String pattern) throws ParseException {
        long day = DateTimeUtils.subtractDateTimeStringToDay(dateTimeA, dateTimeB, pattern);
        if (day == -1) {
            return -1;
        }
        return day / 30;
    }

    public static long isDateTimeString(String dateTimeString, String pattern) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(dateTimeString).getTime();
        } catch (ParseException e) {
            return -1;
        }
    }

    public static String getDateString(String datetime) {
//        Timestamp a = convertStringToTimeStamp2("2014-1-1 0:0:0");
        Timestamp a;
        if (datetime.contains(":")) {
//            Định dạng chuẩn yyyy-MM-dd HH:mm:ss
            a = convertStringToTimeStamp2(datetime);
        } else {
//            Định dạng chuẩn yyyy-MM-dd
            a = convertStringToTimeStamp3(datetime);
        } 
        
        if (a == null) {
            return null;
        }
        Date date = new Date(a.getTime());
        return getDateString(date, "yyyy-MM-dd");
    }
    
    public static String getDatetimeString(String datetime) {
//        Timestamp a = convertStringToTimeStamp2("2014-1-1 0:0:0");
        Timestamp a;
        if (datetime.contains(":")) {
//            Định dạng chuẩn yyyy-MM-dd HH:mm:ss
            a = convertStringToTimeStamp2(datetime);
        } else {
//            Định dạng chuẩn yyyy-MM-dd
            a = convertStringToTimeStamp3(datetime);
        } 
        
        if (a == null) {
            return null;
        }
        Date date = new Date(a.getTime());
        return getDateString(date, "yyyy-MM-dd HH:mm:ss");
    }

}
