java 日期工具类

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

public class Utility {
 
 static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 static DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 static Calendar currentCalendar = Calendar.getInstance();
 static DecimalFormat decimalFormat = new DecimalFormat("#.00");
 
 public static Date currentDateTime() {
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  
  return currentCalendar.getTime();
 }
 
 public static String dateToString(Date date,boolean withTime) {
  
  if(date == null){
   currentCalendar.setTimeInMillis(System.currentTimeMillis());
   date = currentCalendar.getTime();
  }  
  
  return withTime ? dateTimeFormat.format(date) :dateFormat.format(date);
 } 

 public static Date stringToDate(String sdate){
  
  try {
   return isNullOrEmpty(sdate) ? currentDateTime() : dateFormat.parse(sdate);
  } catch (ParseException e) {
   return currentDateTime();
  }
 } 
 
 public static boolean before(String d1,String d2){
  return stringToDate(d1).before(stringToDate(d2));
 }
 
 public static boolean after(String d1,String d2){
  return stringToDate(d1).after(stringToDate(d2));
 }
 
 public static String getYearBegin(String year){
  try {
   if(!isNullOrEmpty(year))
    currentCalendar.set(Calendar.YEAR, Integer.parseInt(year));
   else
    currentCalendar.setTimeInMillis(System.currentTimeMillis()); 
  } catch (NumberFormatException e) {
   currentCalendar.setTimeInMillis(System.currentTimeMillis());  
  }
  currentCalendar.set(Calendar.MONTH, 0);
  currentCalendar.set(Calendar.DATE, 1);
  return dateToString(currentCalendar.getTime(),false);  
 }
 
 public static String getYearEnd(String year){
  try {
   if(!isNullOrEmpty(year))
    currentCalendar.set(Calendar.YEAR, Integer.parseInt(year));
   else
    currentCalendar.setTimeInMillis(System.currentTimeMillis()); 
  } catch (NumberFormatException e) {
   currentCalendar.setTimeInMillis(System.currentTimeMillis());  
  }
  currentCalendar.set(Calendar.MONTH, 11);
  currentCalendar.set(Calendar.DATE, 31);
  return dateToString(currentCalendar.getTime(),false);  
 }
 
 public static String getYearBegin(){
  return getYearBegin(null);  
 }
 
 public static String getYearEnd(){
  return getYearEnd(null);  
 }
 
 public static String currentDateString(){
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  
  return dateFormat.format(currentCalendar.getTime());
 }
 
 public static String currentDateTimeToString() {
        currentCalendar.setTimeInMillis(System.currentTimeMillis());
       
        return dateTimeFormat.format(currentCalendar.getTime());
 }
 
 public static String getDateFlag(boolean withMonth) {
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  
  return getDateFlag(currentCalendar.getTime(),withMonth);
 }
 
 public static String getDateFlag(String dateString){
  
  return (dateString!=null||!"".equals(dateString)) ? dateString.replaceAll("-", "").substring(0,6) : getDateFlag(true);
 }
 
 public static String getDateFlag(String dateString,boolean withMonth){
  
  return (dateString!=null||!"".equals(dateString)) ? dateString.replaceAll("-", "").substring(0, withMonth ? 6 : 4) + (withMonth ? "":"00") : getDateFlag(withMonth);
 }
 
 public static String getDateFlag(Date date){
  
  return getDateFlag(date,true);
 }
 
 public static String getDateFlag(Date date,boolean withMonth){
  
  return (date!=null)? dateFormat.format(date).replaceAll("-", "").substring(0, withMonth ? 6 : 4) + (withMonth ? "":"00") : getDateFlag(withMonth);
 }
 
 @SuppressWarnings("unchecked")
 public static boolean isNullOrEmpty(Collection c){
  return (c == null||c.size()==0);
 }
 
 public static boolean isNullOrEmpty(String s){
  return (s == null||s.length()==0);
 }
 
 public static String getCurrentYear(){
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  
  return currentCalendar.get(Calendar.YEAR)+"00";
 }
 
 public static String getNextYear(String yearFlag){
  
  int year = Integer.parseInt(yearFlag.substring(0, 4));
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  currentCalendar.set(Calendar.YEAR, year+1);
  
  return currentCalendar.get(Calendar.YEAR)+"00";
 }
 
 public static String getNextMonthDateFlag(){
  
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  currentCalendar.add(Calendar.MONTH, 1);
  
  return getDateFlag(currentCalendar.getTime(),true);
 }
 
 public static Integer getCurrentMonth(){
  currentCalendar.setTimeInMillis(System.currentTimeMillis());
  
  return currentCalendar.get(Calendar.MONTH)+1;
 }
 
 public static String format(double number) throws ParseException{
  return decimalFormat.format(number);
 }
 
 public static Number parse(String source) throws ParseException{
  return decimalFormat.parse(source);
 }
 
 public static String changeMonth(int i){
  String month = "";
  if(i<10){
   month = "0"+i;
  }else {
   month = String.valueOf(i);
  }
  return month;
 }
 
 public static BigDecimal toBigDecimal(String value){
  try {
   return BigDecimal.valueOf(Double.valueOf(value).doubleValue());
  } catch (Exception e) {
   return new BigDecimal(0);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值