DateFormat

java 时间输出:

package test.DateFormat;

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

public class DateFormat {
	
	public static void main(String str[]){
		
		/**
		 * 系统currentTimeMillis()
		 */
		System.out.println( "从1970年1月1日开始到系统当前时间之间的毫米数==" + System.currentTimeMillis() );//从1970年1月1日开始到系统当前时间之间的毫米数
		
		/**
		 * Date()
		 */
		Date date = new Date(); //生成Date时,实际上它会使用System.currentTimeMillis()来取得系统时间;
		System.out.println("date 毫秒数换成默认时间格式"+date.toString());//toString()方法时,会将取得的1970年1月1日至今的毫秒数转为dow mon dd hh:mm:ss zzz yyyy的格式,分别是:“星期 月 日 时:分:秒 西元”;
	    System.out.println("date 取得的毫秒数"+date.getTime());//使用Date的getTime()方法则可以取得毫秒数。
		
		/**
		 * DateFormat
		 */
	    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义输出时间的格式
		String login_date = df.format(new Date());
		System.out.println("定义格式的时间"+login_date);
		
		
		/**
		 * 日历类Calendar 
		 */
		
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		System.out.println("当前日期:"+new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime()));
		cal.add(Calendar.DAY_OF_MONTH, -5);
		System.out.println("当前日历减去五天:"+new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime()));
		
		
		cal = Calendar.getInstance(Locale.CHINA);
		int weekday = cal.WEDNESDAY;
		cal.add(Calendar.DAY_OF_MONTH, -weekday+2);
		System.out.println("当前周的周一日期为:"+new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime()));
		

		cal = Calendar.getInstance(Locale.CHINA);
		int nowweekday = cal.WEDNESDAY;
		int allweekday = cal.DAY_OF_WEEK;
		cal.add(Calendar.DAY_OF_MONTH, allweekday-nowweekday+1);
		System.out.println("当前周的周天日期为:"+new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime()));
		
	}
	
}

  输出结果:

从1970年1月1日开始到系统当前时间之间的毫米数==1381300825144

date 毫秒数换成默认时间格式Wed Oct 09 14:40:25 CST 2013

date 取得的毫秒数1381300825144

定义格式的时间2013-10-09

当前日期:2013-10-09

当前日历减去五天:2013-10-04

当前周的周一日期为:2013-10-07

当前周的周天日期为:2013-10-13

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值