java基础:java中的常用类——包装类,日期和时间类以及格式化类

包装类

所谓的包装就是将int,float,char等共8种不同基本数据类型转化成类的形式

基本数据类型对应的包装类
byteByte
charChar
intInteger
longLong
shortShort
floatFloat
douleDoule
booleanBoolean
public class Example006 {
    public static void main(String[] args)
    {
        int a = 20;
        Integer b = a;
        System.out.println(b);
        int c = b;
        System.out.println(c);
    }
}
public class Example006 {
    public static void main(String[] args)
    {
        //String.valueOf()的方法
        int n = 123;
        String str = "996";
        
        String string = String.valueOf(n);
        System.out.println("将int变成字符串"+string);
        
        //通过包装类的静态方法valueOf()
        Integer integer = Integer.valueOf(n);
        Integer integer2 = Integer.valueOf(str);
        System.out.println("将int变量转换成包装类"+integer);
        System.out.println("将字符串变量转换成包装类"+integer2);
        
        //通过包装类的有参基本构造方法
        Integer integer3 = new Integer(n);
        Integer integer4 = new Integer(str);
        System.out.println("用构造方法来转换int的包装类"+integer3);
        System.out.println("用构造方法来转换字符串的包装类"+integer4);
        
        //通过包装类的parsexxx()的静态方法
        int parseInt = Integer.parseInt(str);
        System.out.println("将字符串转换成基本类型"+parseInt);
        
        //通过包装类的toString()方法
        String string2 = integer.toString();
        System.out.println("将包装类转换成字符串"+string2);


    }
}

日期和时间类

Date类

这个类就两个功能,一个构造方法是获取当前的时间,然后还有一个含参的就是获取指定的日期。

import java.util.Date;
import java.util.Random;

public class Example006 {
    public static void main(String[] args)
    {
        Date d1 = new Date();
        Date d2 = new Date(System.currentTimeMillis()+1000);
        System.out.println(d1);
        System.out.println(d2);
        
    }
}

Calendar类

本类是一个抽象类,不能直接实例化,必须使用getInstance()方法以后再使用其他方法。

import java.util.Calendar;

public class Example007 {
    public static void main(String[] args) {
       Calendar calendar = Calendar.getInstance();
       calendar.set(2019,1,1);
       calendar.add(Calendar.DATE,100);
       int year = calendar.get(Calendar.YEAR);
       int month = calendar.get(Calendar.MONTH)+1;
       int date = calendar.get(Calendar.DATE);
        System.out.println("计划竣工日期"+year+"年"+month+"月"+date+"日");


    }
}

日期、时间类

import java.time.*;
import java.util.Calendar;
import java.util.Locale;

public class Example008 {
    public static void main(String[] args) {
       Clock clock = Clock.systemUTC();
        System.out.println("获取UTC时区转换的当前时间"+clock.instant());
        System.out.println("获取UTC时区转换的毫秒数"+clock.millis());

        Duration duration = Duration.ofDays(1);
        System.out.println("一天等于"+ duration.toHours());
        System.out.println("一天等于"+duration.toMinutes());
        System.out.println("一天等于"+duration.toMillis());

        Instant instant = Instant.now();
        System.out.println("获取UTC时区的当前时间"+instant);
        System.out.println("当前时间一小时后的时间"+instant.plusSeconds(3600));
        System.out.println("当前时间一小时前的时间"+instant.minusSeconds(3600));

        LocalDate localDate = LocalDate.now();
        System.out.println("从默认时区的系统时钟获得的当前日期"+localDate);
        LocalTime localTime = LocalTime.now();
        System.out.println("从默认时区的系统时钟获取当前时间"+localTime);
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("从默认时区的系统时钟获取日期,时间"+localDateTime);
        LocalDateTime time = localDateTime.plusDays(1).plusHours(3).plusMinutes(30);
        System.out.println("当前的日期,时间加上1天3小时30分以后"+time);

        Year year = Year.now();
        System.out.println("当前年份"+year);
        YearMonth yearMonth = YearMonth.now();
        System.out.println("当前年月"+yearMonth);
        MonthDay monthDay = MonthDay.now();
        System.out.println("当前月日"+monthDay);
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("当前系统默认时区为"+zoneId);



    }
}

格式化类

DateFormat类

在使用Date类的时候输出的是英文格式,要想转换成中文格式,就需要使用本类进行转换。

import java.text.DateFormat;
import java.util.Date;

public class Example009 {
    public static void main(String[] args) {
        Date date = new Date() ;
        DateFormat fullmate = DateFormat.getDateInstance(DateFormat.FULL);
        DateFormat longmate = DateFormat.getDateInstance(DateFormat.LONG);
        DateFormat shortmate = DateFormat.getDateInstance(DateFormat.SHORT);
        DateFormat mediummate = DateFormat.getDateInstance(DateFormat.MEDIUM);

        System.out.println("完整日期"+fullmate.format(date));
        System.out.println("长日期"+longmate.format(date));
        System.out.println("短日期"+shortmate.format(date));
        System.out.println("普通日期"+mediummate.format(date));
    }
}

SimpleDateFormat类

SimpleDateFormat类是DateFormate的子类,可以实例化。可以将日期形式的字符串转换成各种形式的字符串。

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

public class Example009 {
    public static void main(String[] args) {
        SimpleDateFormat s1 = new SimpleDateFormat("Gyyyy年MM月dd日:今天是yyyy年M月d日,E");
        System.out.println(s1.format(new Date()));

    }
}

DateTimeFormatter类
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;

public class Example009 {
    public static void main(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        System.out.println("使用常量创立DateTimeFoormatter:");
        DateTimeFormatter d1 = DateTimeFormatter.ISO_DATE_TIME;
        System.out.println(d1.format(date));

        System.out.println("使用MEDIUM类型风格的方式创立DateTimeFormatter:");
        DateTimeFormatter d2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        System.out.println(d2.format(date));

        System.out.println("根据模式字符串创立DateTimeFormatter:");
DateTimeFormatter d3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(date.format(d3));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值