UtilTools.java 3.91 KB
package com.xkl.tools;

import org.joda.time.DateTime;

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

/**
 * Created by win7 on 2016/11/20.
 */
public class UtilTools {
    /**
     * support time format yyyy-MM-dd'T'HH:mm:ss.SSS
     *
     * @param timestamp
     * @return
     * @throws Exception
     */
    public static int getTime(String timestamp) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.CHINA);
        Date time = format.parse(timestamp);
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(dayFormat.format(time));
    }
    public static Timestamp getTimestamp(String timestamp) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.CHINA);
        Date time = format.parse(timestamp);
        return new Timestamp(time.getTime());
    }
    public static DateTime getDateTime(String timestamp) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
        Date time = format.parse(timestamp);
        return new DateTime(time.getTime());
    }

    public static int getDay(Timestamp timestamp) {
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(dayFormat.format(new Date(timestamp.getTime())));
    }

    public static int getHour(Timestamp timestamp) {
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMddHH");
        return Integer.parseInt(dayFormat.format(new Date(timestamp.getTime())));
    }

    public static long get13Second(){
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        long a=c.getTimeInMillis();
        return a;
    }

    /**
     * 由String转为long型时间,10位
     * @param timestamp
     * @return
     */
    public static long getLongTime(String timestamp){
        long time = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            time = format.parse(timestamp).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 获取当前时间long型,10位
     * @return
     */
    public static long getNow10Second(){
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        long a=c.getTimeInMillis()/1000;
        return a;
    }

    public static long _long(String value) {
        if (value == null || "null".equals(value)) {
            return 0;
        }
        try {
            return Long.parseLong(value.toString());
        } catch (Exception e) {
            return 0;
        }
    }
    public static long _long(Object value) {
        if (value == null || "null".equals(value)) {
            return 0;
        }
        try {
            return Long.parseLong(value.toString());
        } catch (Exception e) {
            return 0;
        }
    }

    public static int _int(String value) {
        if (value == null || "null".equals(value)) {
            return 0;
        }
        try {
            return Integer.parseInt(value.toString());
        } catch (Exception e) {
            return 0;
        }
    }
    public static Double _double(String value) {
        if (value == null || "null".equals(value)) {
            return 0.0;
        }
        try {
            return Double.parseDouble(value.toString());
        } catch (Exception e) {
            return 0.0;
        }
    }

    public static String _string(Object object) {
        if (object == null || "null".equals(object)) {
            return "";
        }
        return object.toString();
    }

    public static void main(String[] args) {
       System.out.println(getLongTime("2016-10-11 22:22:22"));
    }


}