|
|
package com.xkl.tools;
|
|
|
|
|
|
import org.joda.time.DateTime;
|
|
|
|
|
|
import java.sql.Timestamp;
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
public static long get10Second(){
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|