java 得到日期

package com.tianji.job2.web.delegate.share;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class  DateUtil {
    public static final String DateFormat = "yyyy-MM-dd HH:mm";
    private static DateFormat formater = new SimpleDateFormat(DateFormat);
    public static String getTimeByDate(Date date) {
        Calendar today = Calendar.getInstance();
        today.set(today.HOUR_OF_DAY, 0);
        today.set(today.MINUTE, 0);
        today.set(today.SECOND, 0);

        Calendar calendardate = Calendar.getInstance();
        calendardate.setTime(date);
       
        Boolean bb = calendardate.before(today);
        String dateString = " ";
        if (bb) {
            int mouth = calendardate.get(calendardate.MONTH) + 1;
            //System.out.println(getFormatDate(mouth));
            dateString = getFormatDate(mouth) + "-"+ getFormatDate(calendardate.get(calendardate.DAY_OF_MONTH));
        } else
            dateString = getFormatDate(calendardate.get(calendardate.HOUR_OF_DAY)) + ":"+ getFormatDate(calendardate.get(calendardate.MINUTE));
        return dateString;
    }
    private static String getFormatDate(int date){
        return date<10?"0"+date:date+"";
    }
    public static String getMessageDate(Date date){
        if(date == null)return "";
        return formater.format(date);
    }
    public static void main(String[] args) {
        DateUtil util = new DateUtil();
        Calendar today = Calendar.getInstance();
        today.set(today.MONTH, 1);
        System.out.println(util.getTimeByDate(today.getTime()));
        System.out.println(util.getTimeByDate(new Date()));
        System.out.println(DateUtil.getMessageDate(new Date()));
       
       
    }
       
}

你可能感兴趣的:(java,Web)