date日期的简单使用

package cn.itcast.api.a.map;

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

public class date日期的简单使用 {

	public static void main(String[] args) {
		/*1.日期的简单使用  Java.util.Date
		 * 
		 *2.实现日前对象和毫秒值之间的转换  要求转换方法
		 *	long time = System.currentTimeMillis();//用于计算程序运行时间只要将来时时间与结束时间相减
		 *毫秒值转换成日期对象,获取其中的年(year+1900)月(由0-11表示的)日 
		 * **/
		long time = System.currentTimeMillis();
		System.out.println(time);
		
		
		SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd");  
		  Calendar cal = Calendar.getInstance();//获取日期对象  
		//  cal.setTime(tempDate);  
		  System.out.println(ft.format(cal.getTime()));  
		  System.out.println(cal.get(Calendar.YEAR));  
		  System.out.println(cal.get(Calendar.MONTH)+1); 
		  System.out.println(cal.get(Calendar.DATE)); 
		  System.out.println(cal.get(Calendar.SECOND));
		 
		  System.out.println(cal.get(Calendar.DAY_OF_MONTH));//一个月中的第几天
		  //		  int year = cal.get(Calendar.YEAR);
//		  int mouth = cal.get(Calendar.MONTH)+1
		String week = getCNWeek(cal.get(Calendar.DAY_OF_WEEK));
		 System.err.println(week);//一个星期中的第几天(周天开始)
//		想要将这个毫秒值转成我们熟悉的时间,我们查阅API发现date类中日期对象已初始化就会传递一个毫秒值
		/*Date date = new Date(time);//创建了一个日期对象,将已有的毫秒值进行封装,通过日期对象的方法惊醒操作获得我们所需的数据
		System.out.println(date);
		System.out.println(date.getYear());
		System.out.println(date.getMonth());
		System.out.println(date.getDate());
		System.out.println(date.getSeconds());
		System.out.println(date.hashCode());
		*/
		  
		  
		 
		
	}

	public static String getCNWeek(int i) {
		if(i<0||i>7){
			throw new RuntimeException(i+"没有对应的星期!");
		}
		
		String[] week = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
		return week[i];
	}

}

猜你喜欢

转载自blog.csdn.net/mingxu_W/article/details/82945802