import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil
{
public static final String STANDARD_FORMAT="yyyy-MM-dd HH:mm:ss";
/**
* 得到标准格式的当前日期
* @return
*/
public static String date2String()
{
return date2String(new Date());
}
/**
* 得到标准格式的当前日期
* @return
*/
public static String date2String(String dateFormat)
{
return date2String(new Date(),dateFormat);
}
/**
* 按项目默认格式转换指定日期
* @param arg0
* @return
*/
public static String date2String(Date arg0)
{
return date2String(arg0,STANDARD_FORMAT);
}
/**
* 按指定格式转换指定日期
* @param arg0
* @return
*/
public static String date2String(Date arg0,String dateFormat)
{
SimpleDateFormat sf=new SimpleDateFormat(dateFormat);
return sf.format(arg0);
}
/**
* 将日期字符串格式转换为日期格式
* @param source
* @param dateFormat
* @return
* @throws ParseException
*/
public static Date string2Date(String source,String dateFormat)
{
Date date = new Date();
SimpleDateFormat sf=new SimpleDateFormat(dateFormat);
try
{
date= sf.parse(source);
}
catch (ParseException e)
{
e.printStackTrace();
}
return date;
}
/**
* 根据毫秒数和格式化标志,获取日期的字符串形式
* @param milliseconds 毫秒数
* @param mark
* 0:yyyy-MM-dd <br>
* 1:yyyy-MM-dd HH:mm:ss <br>
* 2:yyyy-MM-dd HH:mm:ss.SSS <br>
* @return
*/
public static String formatDateString(long milliseconds,int mark)
{
String formatString = null;
switch(mark)
{
case 0:
formatString = "yyyy-MM-dd";
break;
case 1:
formatString = "yyyy-MM-dd HH:mm:ss";
break;
case 2:
formatString = "yyyy-MM-dd HH:mm:ss.SSS";
break;
default:
formatString = "yyyy-MM-dd";
break;
}
Date date = new Date();
date.setTime(milliseconds);
return date2String(date,formatString);
}
/**
* 得到 几天 后的年-月-日时间格式
* @day 当前时间加几天
* @return
*/
public static String getNMDAddOneDay(int day){
SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, day);
Date d1= cal.getTime();
return dFormat.format(d1);
}
/**
* 获得多少秒后的时间
* @param second
* @return
*/
public static String gettime_second(int second){
SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, second);
Date d1= cal.getTime();
return dFormat.format(d1);
}
/**
* 非中国时间转化为中国时间
* @param d1 美国时间
* @return
*/
public static String getime_byUs(String d1){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date(d1));
}