DateUtils.java 1.79 KB
package com.yoho.message.sdk.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chenchao on 2017/1/13.
 */
public final class DateUtils {
    public static final String dateConnSeperator="-",

     dateFormat = "yyyy-MM-dd",

     fullDateTimeFormat = "yyyy-MM-dd HH:mm:ss",

    MIN_DateTimeFormat = "yyyy-MM-dd HH:mm",

            DateTimeFormat = "yy.MM.dd HH:mm",

                    d_DateTimeFormat = "yy-MM-dd HH:mm";

    public static int getCurrentTimeSeconds() {
        long longTime = System.currentTimeMillis();
        return (int) (longTime / 1000);
    }




    public static long getCurrentDateMillis(){
        StringBuffer dateStr = new StringBuffer();
        Calendar now = Calendar.getInstance();
        int year= now.get(Calendar.YEAR);
        int month= now.get(Calendar.MONTH) + 1;
        int day=now.get(Calendar.DAY_OF_MONTH);
        dateStr.append(year).append(dateConnSeperator)
                .append(month).append(dateConnSeperator)
                .append(day);
        DateFormat format = new SimpleDateFormat(dateFormat) ;
        long dateTime=0L;
        try {
            Date nowDate = format.parse(dateStr.toString());
            dateTime=nowDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.long2DateStr(1514982989 * 1000l, DateUtils.fullDateTimeFormat));

    }

    public static String long2DateStr(long time, String format) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateStr = sdf.format(date);
        return dateStr;
    }
}