分享

封装一个显示各种格式的时间类

 Sunny_Gql 2011-07-25
import java.util.Calendar;
import java.util.Date;


public class TestDate {
    public static Calendar calendar = null;
    public static int year;
    public static int month;
    public static int day;
    public static int hour;
    public static int minute;
    public static int second;
    public static long time;
   
    public TestDate() {
    }

    public static void main(String[] args) {
        init();
       //预演示
        System.out.println("年- 月- 日 时:份:秒===" + getDateTime());
        System.out.println("年- 月- 日 ===" + getDate());
        System.out.println("时:份:秒===" + getTime());
       
    }
    /**
     * @param 进行初始化运行
     **/
    public static void init() {
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = 1 + calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        second = calendar.get(Calendar.SECOND);
    }
/**
     * @param 得到几天后的时间
     * @days  天数
     **/
    public static int getAfterDate(int days){
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+days);  
        return calendar.get(Calendar.DATE);
    }
    /**
     * 取日期-时间 yyyy-mm-dd hh:mm:ss
     */
   public static String getDateTime() {
        String months, days, hours, minutes, seconds;
        if (month < 10) {
            months = "0" + month;
        } else {
            months = String.valueOf(month);
        }

        if (day < 10) {
            days = "0" + day;
        } else {
            days = String.valueOf(day);
        }

        if (hour < 10) {
            hours = "0" + hour;
        } else {
            hours = String.valueOf(hour);
        }

        if (minute < 10) {
            minutes = "0" + minute;
        } else {
            minutes = String.valueOf(minute);
        }

        if (second < 10) {
            seconds = "0" + second;
        } else {
            seconds = String.valueOf(second);
        }

        return year + "- " + months + "- " + days + "   " + hours + ": "
                + minutes + ": " + seconds;
    }

   /**
     * 取日期 yyyy-mm-dd;
     */
   public static String getDate() {
        String months, days;
        if (month < 10) {
            months = "0 " + month;
        } else {
            months = String.valueOf(month);
        }

        if (day < 10) {
            days = "0 " + day;
        } else {
            days = String.valueOf(day);
        }

        return year + "- " + months + "- " + days;
    }

   /**
     * 取时间 hh:mm:ss;
     */
   public static String getTime() {
        String hours, minutes, seconds;
        if (hour < 10) {
            hours = "0 " + hour;
        } else {
            hours = String.valueOf(hour);
        }

        if (minute < 10) {
            minutes = "0 " + minute;
        } else {
            minutes = String.valueOf(minute);
        }

        if (second < 10) {
            seconds = "0 " + second;
        } else {
            seconds = String.valueOf(second);
        }

        return hours + ": " + minutes + ": " + seconds;
    }

   /**
     * 取年 yyyy
     */
   public static int getYear() {
        return (year);
    }

   /**
     * 取月 mm
     */
   public static int getMonth() {
        return (month);
    }

   /**
     * 取日 dd
     */
   public static int getDay() {
        return (day);
    }

   /**
     * 取小时 hh
     */
   public static int getHour() {
        return (hour);
    }

   /**
     * 取分钟 mm;
     */
   public static int getMinute() {
        return (minute);
    }

   /**
     * 取秒钟 ss
     */
   public static int getSecond() {
        return (second);
    }

   /**
     * 取从1970年1月1日以来的时间(毫秒)
     */
   public static long getLongTime() {
        java.util.Date d = new java.util.Date();
        return d.getTime();
    }

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约