public class test08 {
public static void main(String[] args) throws ParseException {
//出生日期
String birthday="1996-06-11";
//今天日期
String now ="2018-04-09";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//将字符串转换为日期
Date date=simpleDateFormat.parse(birthday);
Date date2 =simpleDateFormat.parse(now);
//将日期转换为long的毫秒值
long birthdaymils=date.getTime();
long nowmils=date2.getTime();
Calendar:日历 提供了一些操作年月日时的方法
创建对象
静态方法: static Calendar getInstance()
示例: Calendar c = Calendar.getInstance();
获取 int get(int field) 获取指定的日期字段
修改 void add(int field,int amount):增加或减少指定的日期字段值(调整时间)
calendar.add(calendar.DAY_OF_MONTH,-1);
添加 void set(int field,int value):设置指定的日期字段值(设置时间)
calendar.set(calendar.YEAR, 2000);
Calender 抽象类。日历,提供操作日期字段的方法
static
Calendar getInstance() //获取对象,默认当前时间
int YEAR 年
int MONTH 年中月(0-11)
int DAY_OF_MONTH 月中天
int HOUR 小时(12小时制)
int HOUR_OF_DAY 小时(24小时制)
int MINITE 分钟
int SECOND 秒
int DAY_OF_WEEK 周中天(1-7,周日-周六)
int DAY_OF_WEEK_IN_MONTH 月中周
int DAY_OF_YEAR 年中天
包装类: character Integer(关键词)
就是封装了基本数据类型的类,为我们提供了更多复杂的方法和一些变量
Integer
构造方法
Integer(int value) : int转Integer
Integer(String value) : String转Integer
成员方法
int intValue() : Integer转int
String toString() : Integer转String
static int parseInt(String value) : String转Integer
整数基本类型和包装类转换
int转Integer
利用Integer的构造方法: Integer Integer(int value)
如: Integer i = new Integer(10);
Integer转int
利用Integer的成员方法: int intValue()
如: int a = i.intValue();
字符串和整数转换
String转int/Integer
方式1: Integer Integer(String s)
如: Integer i = new Integer("10");
方式2: static int parseInt(String s)
如: int a = Integer.parseInt("10");
方式3: static Integer valueOf(String s)
如: int b = Integer.valueOf("10");
int/Integer转String
方式1: "" + int值
如: String a = "" + 10;
方式2: String toString()
如: String a = i.toString();
方式3: static String toString(int i)
如: String a = Integer.toString(10);