java 日期函数解释

以下内容引自:http://blog.donews.com/xehu/archive/2005/07/14/465783.aspx

 

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

public class SystemTime {
    public static void main(String args[]){
        SystemTime st = new SystemTime();
        st.getTimeOne();
        st.getTimeTwo();
    }

    /**方法一*/
    public void getTimeOne(){
        Calendar cal = Calendar.getInstance();

        //当前年
        int year = cal.get(Calendar.YEAR);
        //当前月
        int month = (cal.get(Calendar.MONTH))+1;
        //当前月的第几天:即当前日
        int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
        //当前时:HOUR_OF_DAY-24小时制;HOUR-12小时制
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        //当前分
        int minute = cal.get(Calendar.MINUTE);
        //当前秒
        int second = cal.get(Calendar.SECOND);
        //0-上午;1-下午
        int ampm = cal.get(Calendar.AM_PM);
        //当前年的第几周
        int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
        //当前月的第几周
        int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
        //当前年的第几天
        int day_of_year = cal.get(Calendar.DAY_OF_YEAR);

        System.out.print("现在是" + year + "年" + month + "月" + day_of_month + "日" + hour + "时" + minute + "分" + second + "秒");
        System.out.println("(" + (ampm == 0 ? "上午" : "下午") + ")");
        System.out.println("今天是" + year + "的第" + day_of_year + "天");
        System.out.println("本周是" + year + "年的第" + week_of_year + "周," + month + "月的第" + week_of_month + "周");
    }

    /**方法二*/
    public void getTimeTwo(){
        Date date = new Date();
        //时间格式化:MM-月份;mm-分钟;HH-24小时制;hh-12小时制
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //返回英文(中文)月份

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

        System.out.println(df.format(date));
    }
}

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