java 时间Util



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));
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鑫不列迭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值