java-零零妖

导航

获取系统时间

package com.BackStage.util;

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

public class DateUtil {
public final static String FORMAT ="yyyy-MM-dd HH:mm:ss";
/**
* 将Date类型转变为指定格式的字符串
*/
public static String formatString(Date date, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
String nowTime = dateFormat.format(date);
return nowTime;
}
public static String formatString() {
SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT, Locale.CHINA);
Date date = new Date();
String nowTime = dateFormat.format(date);
return nowTime;
}

/**
* 将Date类型转变为指定格式的字符串
*/
public static String formatString(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT, Locale.CHINA);
String nowTime = dateFormat.format(date);
return nowTime;
}

/**
* 字符串转换成日期格式
*
* @param date
* @return
* @throws ParseException
*/
public static Date parseDate(String date)
throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
Date nowTime = dateFormat.parse(date);
return nowTime;
}
public static Date parseDate(String date,String format)
throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
Date nowTime = dateFormat.parse(date);
return nowTime;
}
/**
* 日期加减运算
* @param date 对指定日期运算
* @param calendarField 对日期中的那个类型进行运算,比如1表示对年运算, 5表示对天进行运算
* @param amount 正数表示加,负数表示减
* @return
*/
public static Date add(Date date, int calendarField, int amount) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
}

在业务实现类直接调用就可以

 


posted on 2018-07-20 18:52  java-零零妖  阅读(129)  评论(0编辑  收藏  举报