日期

1.包装类

什么是包装类:
基本数据类型所对应的引用数据类型就是包装类
相互转换:JDK1.5以后自动装箱和拆箱[
基本类型 包装类
int Integer
char Character
其他 首字母大写

5.2 自动装箱与自动拆箱
装箱:基本数据类型转换为包装类对象
拆箱:包装类对象转换为基本数据类型

i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
//加法运算完成后,再次装箱,把基本数值转成对象。

 Integer  i=Integer.valueOf(100);
            Integer ii=100;//自动装箱
            ii+=200;
		//ii=ii+200;
		//ii=ii.intValue()+200;
		//ii=300;相当于先拆箱再装箱
       System.out.println(ii);
        Integer iii=null;
        if(iii!=null){
            iii+=300;
        }

5.3 基本类型与字符串之间的转换
基本类型转字符串:
对应类型的值+" “;
如 34+”"
String转换成对应的基本类型
public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。

Integer i3=Integer.valueOf(100);
        System.out.println(i3);
        Integer i4=Integer.valueOf("100");
        System.out.println(i4);

2.日期时间类

2.1Date
public Date():分配一个Date对象,并对初始化,精确到毫秒
public Date(long date):分配一个Date对象,并将其初始化表示从标准基类时间起指定的毫秒值

public  class DateDemo01{
public static void main(String[] args) {
Date d1=new Date();
System.out.println(d1);

long date=1000*60*60;
Date d2=new Date(date);
  System.out.println(d2);
}
}

public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
public void setTime(long time):设置时间,给的是毫秒值
java System.out.println(d.getTime()); System.out.println(d.getTime()*1.0/1 000/60/60/24/365+"年"); // long time=1000*60*60; long time=System.currentTimeMillis(); d.setTime(time); System.out.println(d);

2.2 SimpleDateFormat:

构造方法 :

  1. public SimpleDateFormat​():构造一个SimpleDateFormat,使用默认模式和日期格式
  2. public SimpleDateFormat​(String pattern):构造一个SimpleDateFormat 使用给定的模式和默认的日期格式
    格式化从Date到String:
    public final String format(Date date):将日期格式化成日期/时间字符串
    解析:从 String 到 Date
    public Date parse​(String source):从给定字符串的开始解析文本以生成日期
public static void main(String[] args) throws ParseException {
       //从date到String
       Date d = new Date();
       //空参使用默认模式和时间模式
       SimpleDateFormat sdf=new SimpleDateFormat();
       //.format方法格式化成字符串
       String s = sdf.format(d);
       System.out.println(s);//2019/2/14 上午9:14
       System.out.println("---------------");

		//从date到String
        //使用带参构造    使用给定的时间模式
       SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日   hh:mm:ss");
       String s1 = sdf1.format(d);
       System.out.println(s1);//2019年2月14日 9:14:20

       //从String到Date
       String  ss="2039-2-14  9:21:36";
       SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
       //.parse方法是将字符串解析日期格式
         Date dd=sdf2.parse(ss);
         System.out.println(dd);
         }

2.3Calendar:
其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();

public static void main(String[] args) {
   Calendar c = Calendar.getInstance();
   System.out.println(c);
   int month = c.get(Calendar.MONTH);
   int year = c.get(Calendar.YEAR);
   int date = c.get(Calendar.DATE);
   System.out.println(year+"年"+(month+1)+"月"+date+"日");
}
}
public class CalendarDemo {
    public static void main(String[] args) {
        //获取日历类对象
        Calendar c = Calendar.getInstance();

        //public int get​(int field):返回给定日历字段的值
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
        //三年后
        //根据日历的规则,将指定的时间量添加或减去给定的日历字段
        // public abstract void add​(int field, int amount):
       c.add(Calendar.YEAR,+3);
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");

        //10年前的15天后
         c.add(Calendar.YEAR,-10);
         c.add(Calendar.DATE,15);
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
        
        
        //public final void set​(int year,int month,int date):
        // 设置当前日历的年月日
        c.set(2050,12,21);
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
    }
}
public class CalendarTest {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个年份:");
        int year= sc.nextInt();
        Calendar c = Calendar.getInstance();
        //月份是从0开始
        c.set(year,2,1);
        c.add(Calendar.DATE,-1);
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年2月份有"+date+"天");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值