首页 > 代码库 > 111

111

package com.leo.quickstart;

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

/**
 * Created by Leo on 2014/5/4.
 */
public class DateTest {

    /**
     * 获取当前时间
     */
    private static void getCurrentTime()
    {
        Calendar now=Calendar.getInstance();	//获取当前时间 2014-03-27
        Integer year=now.get(Calendar.YEAR);	//返回2014
        Integer month=now.get(Calendar.MONTH);	//返回2  注意: Calendar中的月从0开始。
        Integer date=now.get(Calendar.DATE);

        //返回27

        System.out.println(year);
        System.out.println(month);
        System.out.println(date);
    }

    private static void createACalendar()
    {
        //创建一个Calendar对象,表示1985-08-28
        Calendar time=Calendar.getInstance();
        time.set(Calendar.YEAR, 1985);
        time.set(Calendar.MONTH, 8-1);
        time.set(Calendar.DATE, 28);
    }

    /**
     * 将Calendar格式化成String
     */
    private static void fromCalendarToString()
    {
        Calendar cale = Calendar.getInstance();									//使用默认时区和语言环境获得一个日历
        Date time=cale.getTime();												//将Calendar类型转换成Date类型
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	//设置日期输出的格式
        System.out.println(format.format(time));								//格式化输出
    }

    /**
     * 从Calendar到Date
     */
    private static void fromCalendarToDate()
    {
        Calendar calendar=Calendar.getInstance();
        Date date=calendar.getTime();
    }

    /**
     * 从Date到Calendar
     */
    private static void fromDateToCalendar()
    {
        Date date=new Date();
        Calendar cal= Calendar.getInstance();
        cal.setTime(date);
    }

}