zoukankan      html  css  js  c++  java
  • 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类

    初步日期使用方法及格式转换方法(旧方法):

    格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北京时间

    获取当前毫秒

    Date date = new Date();

    转换时间格式:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = sdf.format(date);

    日历类的使用

    Calendar c1 = Calendar.getInstance();
            int year = c1.get(c1.YEAR);
            int month = c1.get(c1.MONTH);

    打印日历示例(新方法:LocalDate时间类)

    public static void getMonth(int year,int month){
    
            LocalDate date = LocalDate.of(year, month, 1);
            /**
             * 判断月
             */
            int days = 0;
            if(month == 1 ||month == 3 ||month == 5||month == 7||month == 8 ||month == 10 || month == 12){
                days = 31;//31天    
            }else if(month == 2){
                //2月份
                if(date.isLeapYear()){
                    days = 29;//29天
                }else{
                    days = 28;//28天
                }
            }else{
                days = 30;//30天
            }
            //判断当前月第一天是周几
            int week = date.getDayOfWeek().getValue();
            /**
             * 一  二  三  四  五  六  日
             *           1 2 3 4 
             * 5 6
             */
            //第一行,打印"日  一  二  三  四  五  六"
            //第二行,打印,根据星期打印空格,
            //之后打印1--(30 31 28 29)
            System.out.println("日	一	二	三	四	五	六");
            //打印空格
            int count = 0;
            for (int i = 1; i < week; i++) {
                count++;
                System.out.print("	");
            }
            //1 ---- 31 29 30
            for (int i = 1; i <=days; i++) {
                count++;//接着之前的空格计算
                System.out.print(i+"	");//不换行打印
                if(count%7==0){//每行7个字符。换行
                    System.out.println();
                }
            }
        }
        public static void main(String[] args) {
            getMonth(2018, 7);
            for(int i=1;i<=12;i++){
                System.out.println("
    *********************************************");
                getMonth(2019, i);
                System.out.println("
    *********************************************");
            }
        }
    
    }

    LocalDate 创建日期类,日期的格式化,快速创建日期的方法,获取年月日,判断闰年的方法

    LocalTime 创建时间的类,时分秒的格式化

    LocalDateTime日期时间类

    DateTimeFormatter类-----"yyyy-MM-dd HH:mm:ss"

    可以说,LocalDateTime里可以包含LocalDate类的信息和LocalTime类的信息

    LocalDate使用示例:

    //判断当前年是否是闰年
            LocalDate localDate1 = LocalDate.now();
            boolean leapYear = localDate1.isLeapYear();
            //2001 1 1    快速的构建一个日期类LocalDate  of()方法去构建
            LocalDate ld2 = LocalDate.of(2001, 1, 1);

    LocalTime使用举例:用来返回当前时间

    LocalDateTime dateTime = LocalDateTime.now();
            
            System.out.println(dateTime);
    //格式化日期格式的类DateTimeFormatter
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss");
            //把LocalDateTime类型转化为String字符串类型
            String strDateTime = dtf.format(dateTime);
    //获取当前月份,获取当前年份,时分秒
            LocalDateTime date = LocalDateTime.now();
            date.getYear();
            date.getMonth();
            date.getHour();
            date.getMinute();
            date.getSecond();

    2.String某些函数用法

    //不区分大小写判断是否相等
    str1.equalsIgnoreCase(str2)
    //字符串是否包含另一个串
    str1.contains("a")
    //找到字符串中下标为1的字符
    str1.charAt(1)
    //截取字符串,包含前不包含后
    String newStr = str.substring(1,5);
    //按逗号分割成String数组
    String[] ss =strs.split(",");
    //trim()方法,只能去前后空格,中间空格去不掉
    String s = str3.trim();
    //检查字符串的尾串是不是逗号
    countstr.endsWith(",")
    //替换所有的逗号为空字符
    countstr.replace(",","");
    //最后一个点的位置
    email.lastIndexOf(".")
    //第一个@的位置
    email.indexOf("@")

    String与StringBuffer的区别

    1、String的值不可以改变,只能改变内存地址的指向。所以String类在修改字符串方面的效率较低。StringBuffer的值可以改变,带有缓冲区,方便进行内容修改。

    2、String类中连接用’+’,而StringBuffer用append方法做数据连接

    StringBuffer类中定义的方法全部使用同步定义,属于线程安全操作

    StringBuilder类没有同步定义,都是异步方法,属于非线程安全操作

    String与StringBuffer相互转换

    StringBuffer strBuff = new StringBuffer("新字符串:");
            for (int i = 0; i < ss.length; i++) {
                strBuff.append(ss[i]+",");//将后面加上逗号,放入缓冲区
            }
            System.out.println(strBuff.toString());
            strBuff.deleteCharAt(strBuff.length()-1);
    //去掉最后一个逗号,不用返回,直接拿strBuff操作
            System.out.println(strBuff.toString());
            //新字符串:今天,天气真的不错,我们一起去爬山吧,好的
            char a[] = new char[]{'[','2','0','1','8',']'};
            strBuff.insert(0,a);
            //strBuff.insert(0, "[2018] ");
            System.out.println(strBuff.toString());

    正则表达式用法

    String reg = "^[0-9]{0,9}";//零到九
            //*所有
            //.占位符
            boolean flag = "12".matches(reg);
            System.out.println(flag);

    数字包装类

    包装类解决了基本数据类型,不能使用对象的问题

    1、  装箱--->把基本数据类型转化成相应的包装类

    Integer i = new Integer(a);
    Integer i1 = new Integer("100");
    Double d1 = new Double(b);
    char c2 = new Character('a');

    自动装箱:

    Integer iii = 1000;
    Character c1 = 'a';
    double b = 10.10;

    2、  拆箱--->把包装类转化为基本数据类型(自动拆箱和手动拆箱)

    手动拆箱:

    int i_3 = i1.intValue();
        double d_2 = d1.doubleValue();
    boolean bool1_2 = bool1.booleanValue();

    自动拆箱

    int i__3 = i;
    int i1__3 = i1;

    3、  String和包装类及基本数据类型的转化

    String str5 = "2312";
    String str6 = "2345.23.23";
    String str7 = "23.12d";
    String str2 = "1000";
    int ii2 = Integer.valueOf(str2);
    //如果str5加上L字符,转化将有异常
    Long str5_1 = Long.valueOf(str5);
    //出现多个点的话,转化有错误
    double str6_1 = Double.valueOf(str6);
  • 相关阅读:
    Postgresql HStore 插件试用小结
    postgres-xl 安装与部署 【异常处理】ERROR: could not open file (null)/STDIN_***_0 for write, No such file or directory
    GPDB 5.x PSQL Quick Reference
    postgresql 数据库schema 复制
    hive 打印日志
    gp与 pg 查询进程
    jquery table 发送两次请求 解惑
    python 字符串拼接效率打脸帖
    postgresql 日期类型处理实践
    IBM Rational Rose软件下载以及全破解方法
  • 原文地址:https://www.cnblogs.com/Andrea-null/p/9471416.html
Copyright © 2011-2022 走看看