package com.scd.core;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeUtil {
/**
* 时间unix转换int转为date
* @param timestampString
* @return
*/
public static String TimeStampDate(String timestampString, String format) {
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new java.text.SimpleDateFormat(format).format(new java.util.Date(timestamp));
return date;
}
/**
* 将时间unix转换为int类型
* @param timeString
* @param format
* @return
*/
public static int DateToInt(String timeString, String format) {
int time = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(timeString);
String strTime = date.getTime() + "";
strTime = strTime.substring(0, 10);
time = Integer.parseInt(strTime);
}
catch (ParseException e) {
e.printStackTrace();
}
return time;
}
/**
* 获取当前日期和时间
*
* @return String
*/
public static String getCurrentDateStr() {
Date date = new Date();
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
str = df.format(date);
return str;
}
public static void main(String[] args) {
int a = TimeUtil.DateToInt("2015-08-25 15:56:56", "yyyy-MM-dd hh:mm:ss");
System.out.println(a);
String a1 = TimeUtil.TimeStampDate("1440489416", "yyyy-MM-dd hh:mm:ss");
System.out.println(a1);
String date = TimeUtil.getCurrentDateStr();
System.out.println(date);
}
}