Date与Calander使用及异同


部分代码非原创,转载而来,互相学习 :)

1. 区别

  • Date 用于格式化等
  • Calander 用于计算

2.Date

Date 表示特定的瞬间,精确到毫秒。用距离1970年1月1日00:00:00的毫秒数(long)类型来表示一个特定的时间点,该值可正可负。
Date类中很多方法已经过时,使用Calendar来替代。

常用方法有

  • long getTime()

  • void setTime(long time)

  		Date date=new Date();
        long current=date.getTime();
        System.out.println(current);//1472002479966
        System.out.println(date);//Wed Aug 24 09:34:39 CST 2016`

3.Calander

Java.util.Calendar是抽象类,主要用来对时间分量进行计算。

Calendar类的成员方法
static Calendar getInstance()使用默认时区和区域设置获取日历。通过该方法生成Calendar对象。如下所示:Calendar cr=Calendar.getInstance():
public void set(int year,int month,int date,int hourofday,int minute,int second)设置日历的年、月、日、时、分、秒。
public int get(int field)返回给定日历字段的值。
public void setTime(Date date)使用给定的Date设置此日历的时间。Date------>Calendar
public Date getTime()返回一个Date表示此日历的时间。Calendar----->Date
abstract void add(int field,int amount)按照日历的规则,给指定字段添加或减少时间量。
public long getTimeInMillies()以毫秒为单位返回该日历的时间值。
  • Calendar getInstance()
public class LearnTimeDemo {
	public static void main(String[] args) {
		long TimeNow = System.currentTimeMillis();
		//获得系统时间,一般用long类型的,得到此事件,currentTimeMillis()方法精确到毫秒
		System.out.println("此刻时间"+TimeNow);
		//输出为long类型的语言,机器能读懂的语言
		
		Calendar calendar=Calendar.getInstance();
		//获得此刻的时间
		System.out.println("Calendar获得的时间"+calendar.getTime());

	}
  • get()
public class Demo3 {
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    
		//获取东八区时间
		 int year = c.get(Calendar.YEAR);   
		  //获取年
		 int month = c.get(Calendar.MONTH) + 1;   
		  //获取月份,会少一天。所以+1
		 int day = c.get(Calendar.DAY_OF_MONTH);    
		  //获取当前天数
		 int t = c.get(Calendar.HOUR_OF_DAY);       
		 //获取当前小时
		 int m = c.get(Calendar.MINUTE);          
		 //获取当前分钟
		 int se = c.get(Calendar.SECOND);          
		 //获取当前秒
		 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 //格式化时间格式
		 String Now = s.format(c.getTime());  
		 //获得当前日期     format(Obj):格式化一个对象以生成一个字符串。
		 System.out.println("当前时间:" + year + "-" + month + "-"+ day + " "+t + ":" + m +":" + se);
		 System.out.println("当前日期Now:" + Now);
	}

}
  • add(),setTime()
public String nextDate(String mm) throws Exception {
        //String mm="2015-05-25";
        SimpleDateFormat aa = new SimpleDateFormat("yyyy-MM-dd");

        Date date = aa.parse(mm); //String to Date

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return (aa.format(calendar.getTime()));

4.SimpleDateFormat

用于格式化时间 Date

package org.maoge.common;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
    public static void main(String[] args) {
        //默认输出格式
        Date date=new Date();
        System.out.println(date);//Fri Oct 27 16:56:37 CST 2017
        //日期格式化显示,首先定义格式
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");//显示20171027格式
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");//显示2017-10-27格式
        SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//显示2017-10-27 10:00:00格式
        SimpleDateFormat sdf4=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");//显示2017年10月27日10时00分00秒格式
        //将格式应用于日期
        System.out.println(sdf1.format(date));//20171027
        System.out.println(sdf2.format(date));//2017-10-27
        System.out.println(sdf3.format(date));//2017-10-27 17:11:13
        System.out.println(sdf4.format(date));//2017年10月27日17时11分13秒
    }
}

5.String、Data、Calendar时间转化

  • 所有涉及转换的必须要抛出异常 / try-catch
  • SimpleDateFormat 只与Date 交互

1.Calendar 转化 String

Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());

2.String 转化Calendar

String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

3.Date 转化String

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());

4.String 转化Date

String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);

5.Date 转化Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());

6.Calendar转化Date

Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();

7.String 转成 Timestamp

Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");

8.Date 转 TimeStamp

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值