Date类,Calendar 类,SimpleDateFormat类
Date类
所在包:import java.util.Date;
Date表示特定的时间,精确到毫秒。可以通过方法设定表示指定时间。
System.currentTimeMillis();//返回的是当前操作系统时间距1970年1月1日之间的毫秒数。
构造方法:
Date():创建的是表示当前系统时间的Date对象
Date(long date):根据指定时间创建Date对象
时间的友好格式化
如:
Date d = new Date();
System.out.println(d.toLocaleString());
Date的常用方法
toLocaleStrring();//时间友好化格式输出;
setTime();//设置时间,以1970年1月1日为起始点,传入毫秒数设置时间;
getTime();//获得据1970年1月1日的时间差;以毫秒表示。
如:
Dated=newDate();
d.setTime(10006060*24);//传入一天的时间;结果会显示为1970年1月2日。
实例如:
/*
* Date类
*/
import java.util.Date;
public class Dome04Date {
public static void main(String[] args) {
//当前电脑时间
Date date=new Date();
System.out.println("电脑默认格式:"+date);//电脑默认格式
//格式友好化(转换后为string格式):toLocaleString()方法
String d2=date.toLocaleString();
System.out.println("友好化:"+d2);
//用毫秒表示时间 1000*60*60*24=1天
Date date3=new Date(1000*60*60*24);
System.out.println(date3.toLocaleString());
}
}
结果:
Calendar 类
Calendar 日历:提供了一些操作年月日时的方法
构造方法:
Calendar c = Calendar.getInstance();//获得当前日历信息
常用方法:
Void set(int field,int value)//将给定的日历字段设置为给定的值。
如:c.set(Calendar.DAY_OF_MONTH,20);//将当前月的日期改为20号;
Void add(int field,int amount)//在指定的字段上加上指定的值。
如:c.add(Calendar.DAY_OF_MONTH,8);//在当月原有日期时间基础上加8
int get(int field)返回给定日历字段的值。
如:
想知道当前日历的年份:
int year=c.get(Calendar.YEAR);
想知道当前日历的月份:
int month=c.get(Calendar.MONTH)+1;//月份是从0开始所以要加1
想知道当前月的日期号:
int day=c.get(Calendar.DAY_OF_MONTH);
Calendar实例:
import java.util.Calendar;
/*
* Calendar类
*/
public class Dome03Calendar {
public static void main(String[] args) {
//当前日历
Calendar c = Calendar.getInstance();
//获取年月日
int year = c.get(Calendar.YEAR);
//月份是从0开始的,所以计算月份时要加1
int month = c.get(Calendar.MONTH)+1;
int day=c.get(Calendar.DAY_OF_MONTH);
System.out.println("当前日历为:"+year+"年"+month+"月"+day+"日");
//设置月
c.set(Calendar.MONTH, 9);
//设置日期方法1:
c.set(Calendar.DAY_OF_MONTH,2);
//设置日期方法2:在你的得到日期后面加上5
// c.add(Calendar.DAY_OF_MONTH,5);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH)+1;
day=c.get(Calendar.DAY_OF_MONTH);
System.out.println("当前日历为:"+year+"年"+month+"月"+day+"日");
}
}
SimpleDateFormat类
作用:可以自定义模式构造要输出的年月日和时间的格式
构造方法:
SimpleDateFormat() 使用默认模式进行对象构建、
如:SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat(String pattern) 使用指定模式进行对象构建
如:
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日”);
日期对象转换成字符串
使用方法format();在传入到SimpleDateFormat()中。
字符串转换成Date对象
使用parse();方法
注:
parse()方法是SimpleDateFormat类所有的,所以在使用此方法是要用SimpleDateFormat的对象调用,不能使用Date.parse()形式使用。
实例:
/*
* SimpleDateFormat类
*自定义要输出的时间格式
*/
public class Dome05_SimpleDateFormat {
public static void main(String[] args) throws ParseException {
//创建SimpleDateFormat对象
SimpleDateFormat sdf=new SimpleDateFormat();
//创建Date对象
Date date=new Date();
//将Date类型转为字符串
String s=sdf.format(date);
System.out.println("sdf默认格式:"+s);
//将字符串转为Date类型
Date date2=sdf.parse(s);
System.out.println("Date默认格式:"+date2);
System.out.println("Date的友好格式:"+date2.toLocaleString());
//自定义格式输出1
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd日");
String s2 = sdf2.format(date2);
System.out.println("自定义格式1:"+s2);
//自定义格式输出2
SimpleDateFormat sdf3=new SimpleDateFormat("yyyy年MM月dd日 HH,mm,ss");
String s3=sdf3.format(date2);
System.out.println("自定义格式2:"+s3);
}
}
运行结果: