Date类/Calendar类/SimpleDateFormat类

package cn.itcast.other;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*
日期类 Date 
 
Calendar
 
 
日期格式化类    SimpleDateFormat
 
 */
public class Demo3  {
	
	public static void main(String[] args) throws ParseException {
		/*Date date = new Date(); // 获取当前的系统时间
		System.out.println("年份:"+ date.getYear());*/
		/*
		Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。
		System.out.println("年:"+ calendar.get(Calendar.YEAR));
		System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));
		System.out.println("日:"+ calendar.get(Calendar.DATE));
		
		System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("分:"+ calendar.get(Calendar.MINUTE));
		System.out.println("秒:"+ calendar.get(Calendar.SECOND));
		
		// 显示 当前系统时间: 2014年12月26日  xx时xx分xx秒   
		
		 *  日期格式化类    SimpleDateFormat 
		 *  		作用1: 可以把日期转换转指定格式的字符串     format()
		 *  		作用2: 可以把一个 字符转换成对应的日期。    parse()   生日
		 *  	
		 */
		Date date = new Date(); //获取当前的系统时间。
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象。
		String time = dateFormat.format(date);  //可以把日期转换转指定格式的字符串
		System.out.println("当前的系统时间:"+ time);
		
		String birthday = "2000年12月26日   11:29:08";
		Date date2 = dateFormat.parse(birthday);  //注意: 指定的字符串格式必须要与SimpleDateFormat的模式要一致。
		System.out.println(date2);
		
		Date date21 =new Date();
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
		String time2 =dateFormat.format(date21);
		String time21=dateFormat.format(date);
		System.out.println("当前的系统时间:"+time);
		String birthday1= "2000年12月26日  11:28:08";
		Date date22=dateFormat.parse(birthday1);
		System.out.println(date22);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值