ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P486大数据处理-P497Date

2022-01-16 21:01:43  阅读:110  来源: 互联网

标签:String P497Date System P486 println str 数据处理 Calendar out


P486大数据处理

image-20220116091219432

//当我们编程中,需要处理很大的整数,long 不够用
// 可以使用BigInteger 的类来搞定
// long l = 23788888899999999999999999999l;
// System.out.println("l=" + l);
BigInteger bigInteger = new BigInteger("10000");
//老韩解读
// 1. 在对BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行+ - * /
BigInteger bigInteger2 = new BigInteger("100");
// 2. 可以创建一个要操作的BigInteger 然后进行相应操作
BigInteger add = bigInteger.add(bigInteger2);
System.out.println(add);//输出10100
BigInteger subtract = bigInteger.subtract(bigInteger2);
System.out.println(subtract);//减9900
BigInteger multiply = bigInteger.multiply(bigInteger2);
System.out.println(multiply);//乘1000000
BigInteger divide = bigInteger.divide(bigInteger2);
System.out.println(divide);//除100
//同理高精度的时候,使用Big  BigDecial divide= bigDecial.divide(bigdecial);
//可能抛出异常ArithmeticException
//在调用divide 方法时,指定精度即可. BigDecimal.ROUND_CEILING
如果有无限循环小数,就会保留分子的精度
//System.out.println(bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_CEILING));

P487Date

image-20220116093143237

IDEA diagram properies_展示get和set方法

public class Hello  {
    public static void main(String[] args) throws ParseException {
        //老韩解读
        // 1. 获取当前系统时间
        // 2. 这里的Date 类是在java.util 包
        // 3. 默认输出的日期格式是国外的方式, 因此通常需要对格式进行转换
            Date d1 = new Date(); //获取当前系统时间,输出当前日期=Sun Jan 16 09:45:41 CST 2022
            System.out.println("当前日期=" + d1);
            Date d2 = new Date(9234567); //通过指定毫秒数得到时间
            System.out.println("d2=" + d2); //获取某个时间对应的毫秒数
        // 输出d2=Thu Jan 01 10:33:54 CST 1970
        // 老韩解读
        // 1. 创建SimpleDateFormat 对象,可以指定相应的格式
        // 2. 这里的格式使用的字母是规定好,不能乱写
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年MM 月dd 日hh:mm:ss E");
            String format = sdf.format(d1); // format:将日期转换成指定格式的字符串
            System.out.println("当前日期=" + format);
            //输出当前日期=2022 年01 月16 日09:45:41 周日
        // 老韩解读
        // 1. 可以把一个格式化的String 转成对应的Date
        // 2. 得到Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换
        // 3. 在把String -> Date , 使用的sdf 格式需要和你给的String 的格式一样,否则会抛出转换异常
            String s = "1996 年01 月01 日10:20:30 星期一";
           Date parse = sdf.parse(s);//可以抛出异常throws ParseException
          System.out.println("parse=" + sdf.format(parse));
          //输出parse=1996 年01 月01 日10:20:30 周一
    }
}

P489日期类

image-20220116100803685

//Calendar
//老韩解读
// 1. Calendar 是一个抽象类, 并且构造器是private
// 2. 可以通过getInstance() 来获取实例
// 3. 提供大量的方法和字段提供给程序员
//4. Calendar 没有提供对应的格式化的类,因此需要程序员自己组合来输出(灵活)
// 5. 如果我们需要按照24 小时进制来获取时间, Calendar.HOUR ==改成=> Calendar.HOUR_OF_DAY
//应用实例
Calendar c = Calendar.getInstance(); //创建日历类对象//比较简单,自由
System.out.println("c=" + c);//此时输出大量的字段,所以需要获取日历对象的某个日历字段
//2.获取日历对象的某个日历字段
System.out.println("年:" + c.get(Calendar.YEAR));//输出: 年:2022
// 这里为什么要+ 1, 因为Calendar 返回月时候,是按照0 开始编号
System.out.println("月:" + (c.get(Calendar.MONTH) + 1));//输出: 月:1
System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH));//输出:日:16
System.out.println("小时:" + c.get(Calendar.HOUR));//小时:10
System.out.println("分钟:" + c.get(Calendar.MINUTE));//分钟:16
System.out.println("秒:" + c.get(Calendar.SECOND));//秒:7
//Calender 没有专门的格式化方法,所以需要程序员自己来组合显示
System.out.println(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" +
        c.get(Calendar.DAY_OF_MONTH) +
        " " + c.get(Calendar.HOUR_OF_DAY) + ":" + 
        c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) );
//输出2022-1-16 10:16:7


//第三代日期
// 老韩解读
// 1. 使用now() 返回表示当前日期时间的对象
LocalDateTime ldt = LocalDateTime.now(); //LocalDate.now();//LocalTime.now()
System.out.println(ldt);//输出2022-01-16T10:33:00.586691100
//以下输出年=2022,月=JANUARY,月=1,日=16,时=10,分=33,秒=0
System.out.println("年=" + ldt.getYear());
System.out.println("月=" + ldt.getMonth());
System.out.println("月=" + ldt.getMonthValue());
System.out.println("日=" + ldt.getDayOfMonth());
System.out.println("时=" + ldt.getHour());
System.out.println("分=" + ldt.getMinute());
System.out.println("秒=" + ldt.getSecond());
LocalDate now1 = LocalDate.now(); //可以获取年月日
System.out.println(now1.getYear()+ ","+now1.getMonthValue()+
        ","+now1.getDayOfYear());//输出2022,1,16
LocalTime now2 = LocalTime.now();//获取到时分秒

image-20220116105415973

LocalDateTime ldt = LocalDateTime.now(); //LocalDate.now();//LocalTime.now()
System.out.println(ldt);//输出2022-01-16T10:33:00.586691100
// 2. 使用DateTimeFormatter 对象来进行格式化
// 创建DateTimeFormatter 对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(ldt);
System.out.println("格式化的日期=" + format);
//输出格式化的日期=2022-01-16 10:52:05

P492instance时间戳

image-20220116110945491

//1.通过静态方法now() 获取表示当前时间戳的对象
Instant now= Instant.now();
 System.out.println(now);//输出2022-01-16T04:52:57.900639800Z
 // 2. 通过from 可以把Instant 转成Date
 Date date = Date.from(now);
 //3. 通过date 的toInstant() 可以把date 转成Instant 对象
 Instant instant = date.toInstant();//date转Instance会丢失精度


LocalDateTime ldt = LocalDateTime.now(); //LocalDate.now();//LocalTime.now()
// 2. 使用DateTimeFormatter 对象来进行格式化
// 创建DateTimeFormatter 对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(ldt);
// 提供plus 和minus 方法可以对当前时间进行加或者减
// 看看890 天后,是什么时候把年月日-时分秒
LocalDateTime localDateTime = ldt.plusDays(890);
System.out.println("890 天后=" + dateTimeFormatter.format(localDateTime));
//输出890 天后=2024-06-24 14:40:00
//看看在3456 分钟前是什么时候,把年月日-时分秒输出
LocalDateTime localDateTime2 = ldt.minusMinutes(3456);
System.out.println("3456 分钟前日期=" + dateTimeFormatter.format(localDateTime2));
//输出3456 分钟前日期=2022-01-14 05:04:00

P494练习

//翻转数组
public static void main(String[] args) {
    String str ="abcdef";
    try {
        str= reverse(str,1,41);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return;
    }
    System.out.println(str);
}
public static String reverse(String str,int start,int end){
    if(str != null && start>=0 && end<str.length()){
        throw new RuntimeException("参数错误");
    }
    char[]chars=str.toCharArray();
    char temp=' ';
    for (int i = start,j=end; i <j ; i++,j--) {
        temp=chars[i];
        chars[i]=chars[j];
        chars[j]=temp;
    }
    return new String(chars);
}

P494java注册处理题(注册表的设计)

public class Hello {
    public static void main(String[] args) {
        String un= new Scanner(System.in).next();
        String pd= new Scanner(System.in).next();
        String em= new Scanner(System.in).next();
        try {
            test(un,pd,em);
            System.out.println("注册成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public static void test(String un,String pd,String em){
        if(!(un!=null&&pd !=null && em !=null)){
            throw new RuntimeException("参数为空");
        }
        if(un.length() < 2 && un.length()>4){
            throw new RuntimeException("名字长度错误");
        }
        if(!(pd.length() == 6 && isDi(pd))){
            throw new RuntimeException("密码长度不为6要求全为数字");
        }
        int i=em.indexOf('@');
        int j=em.indexOf('.');
        if(!(i>0&& i > j)){
            throw new RuntimeException("邮箱参数错误,@不在.的前面");
​
        }
      
​
    }
    public static Boolean isDi(String str){
        char[]chars=str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]<'0'|| chars[i]>'9'){
                return false;
            }
        }
        return true;
    }
}
//输出名字
public class Hello {
    public static void main(String[] args) {
        String str="Han Shun Ping";
        PN(str);
    }
    public static void PN(String str){
        if(str==null){
            System.out.println("不能为空");
            return;
        }
        String[] arr=str.split(" ");
        if(arr.length !=3){
            System.out.println("输入的字符串格式不对");
            return;
        }
        char S=arr[1].toUpperCase().charAt(0);
        String format="%s,%s.%c";
        System.out.println(String.format(format,arr[2],arr[0],S));
    }
}

P495两种方法

public class Hello {
    public static void main(String[] args) {
        String str="HanShunPing1";
        PN(str);
    }
    public static void PN(String str){
        if (str==null){
            System.out.println("为空");
            return;
        }
        char [] chars=str.toCharArray();
        int c1=0;
        int c2=0;
        int c3=0;//49-57
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]>=49 && chars[i]<=57){
                c3++;
            }else if(chars[i]>=65 && chars[i]<=90){
                c1++;
            }else if(chars[i]>=97 && chars[i]<=122){
                c2++;
            }else{
            }
        }
        System.out.println("大写字母="+c1+" 小写字母"+c2+" 数字="+c3);
    }
}
for (int i = 0; i < str.length(); i++) {
    if(str.charAt(i)>=0 && str.charAt(i)<=9){
        c3++;
    }else if(str.charAt(i)>='A' && str.charAt(i)<='Z'){
        c1++;
    }else if(str.charAt(i)>='a'&& str.charAt(i)<='z'){
        c2++;
    }else{
    }
}

P496String的内部测试题,两个考点

image-20220116204104424

Sout(a.equals(b));分析:这个equals调用父类的e方法,所以比较地址

String t1="hello"+s1;分析:这里因为s1是变量,所以在堆里开空间,t1返回堆里地址

t1.inter();分析:其实这里指向的是常量池,所以最后为T

标签:String,P497Date,System,P486,println,str,数据处理,Calendar,out
来源: https://blog.csdn.net/kuimeiyuzu/article/details/122528590

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有