重生之我是Java小白五

常用类

一object类

1.1 object类的概述

1.类层次结构的根类

2.所有类都直接或者间接的继承自该类

3.Object类:是java中所有的类共同的父类

4.观察包所属后发现,Object类属于java.lang包下的类,今后使用的时候,不需要进行导包

1.2 object的构造方法

Object() 无参构造方法

1.3object的成员方法

1.3.1 public int hashCode()

1.作用

返回对象的哈希码值

2.使用格式

对象名.hashCode(),返回的是地址值,可以拿变量接收

3.使用场景

可以间接的判断2个对象是否为同一个对象或者说两个的地址值是否一样。

如果是对象直接赋值,那么地址值一样,如果是new出来的新对象,那么地址值不一样

package com.shujia.day10;
class Student{

}

public class ObjectDemo1 {
    public static void main(String[] args) {

        //创建学生对象
        Student student = new Student();
        System.out.println(student.hashCode());  //1163157884

        Student student1 = student;
        System.out.println(student1.hashCode()); //1163157884

        Student student2 = new Student();
        System.out.println(student2.hashCode()); //1956725890
    }
}

1.3.2  public final Class getClass()

1.作用

获取一个类对应的Class对象

2.使用格式

对象名.getClass()  ,返回的是类对应的Class对象,可以拿Class的对象去接收

Class c1=对象名.getClass()

package com.shujia.day10;
class Student{

}

public class ObjectDemo1 {
    public static void main(String[] args) {

        //创建学生对象
        Student student = new Student();
        System.out.println(student.hashCode());  //1163157884
        System.out.println(student.getClass());  //class com.shujia.day10.Student
        Class c1=student.getClass();
        System.out.println(c1);   //class com.shujia.day10.Student

//        Student student1 = student;
//        System.out.println(student1.hashCode()); //1163157884
//
//        Student student2 = new Student();
//        System.out.println(student2.hashCode()); //1956725890
    }
}

1.3.3 public String toString()

1.作用

输出一个对象,实际上输出的是对象调用的toString()方法

2.使用格式

对象名.toString()

3.返回形式

返回一个由其中的对象是一个实例,该符号字符`的类的名称的字符串@ ”和对象的哈希码的无符号的十六进制表示

getClass().getName() + '@' + Integer.toHexString(hashCode())


package com.shujia.day10;
class Student{

}

public class ObjectDemo1 {
    public static void main(String[] args) {

        //创建学生对象
        Student student = new Student();
        System.out.println(student.toString());

        System.out.println(student.getClass().getName()+"@"+Integer.toHexString(student.hashCode()));


    }
}

4.toString()重写object的方法

输出的是类中成员变量的值

package com.shujia.day10;
class Student{
    String name;
    int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class ObjectDemo1 {
    public static void main(String[] args) {

        //创建学生对象
//        Student student = new Student();
//        System.out.println(student.toString());
        Student student = new Student("huangdd", 18);
        System.out.println(student);
        //Student{name='huangdd', age=18}


//        System.out.println(student.getClass().getName()+"@"+Integer.toHexString(student.hashCode()));


    }
}

5.一个标准类的最终写法

 1、成员变量私有化

2、构造方法(无参/所有参数)

3、getXxx()和setXxx()

4、toString()

1.3.4  public boolean equals(Object obj)

1.作用

判断对象是否相等

2.使用格式

对象名.equals(对象名)

3.单纯比较2个对象地址值是否等到 等于 对象名==对象名

package com.shujia.day10;

import java.util.Objects;

class Studend1{
    String name;
    int age;

    public Studend1() {
    }

    public Studend1(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Studend1 studend1 = (Studend1) o;
//        return age == studend1.age && Objects.equals(name, studend1.name);
//    }


}

public class ObjectDemo2 {
    public static void main(String[] args) {
        Studend1 studend1 = new Studend1("huangdd", 18);
        Studend1 studend2 = new Studend1("huangdd", 18);
        System.out.println(studend1.equals(studend2));
        System.out.println(studend1==studend2);
    }
}

4.想比较对象里面的成员变量需要重写 equals方法

package com.shujia.day10;

import java.util.Objects;

class Studend1{
    String name;
    int age;

    public Studend1() {
    }

    public Studend1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Studend1 studend1 = (Studend1) o;
        return age == studend1.age && Objects.equals(name, studend1.name);
    }


}

public class ObjectDemo2 {
    public static void main(String[] args) {
        Studend1 studend1 = new Studend1("huangdd", 18);
        Studend1 studend2 = new Studend1("huangdd", 18);
        System.out.println(studend1.equals(studend2));
        System.out.println(studend1==studend2);
    }
}

1.3.5 protected void finalize()

1.用于垃圾回收的  

当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。

1.3.6 protected Object clone()

1.作用

拷贝一个对象出来 这个对象是属于Object的对象

2.使用

Object 类名=已有的对象名.clone()

但是要重写这个方法,且抛出这个异常

且main方法里面也要抛出这个异常

3.拷贝出来的对象与已有的对象地址值不同

package com.shujia.day10;
//class Demo {
//    int a = 20;
//}

class Student3 implements Cloneable {
    String name;
    int age;
//    Demo demo;

    public Student3() {
    }

    public Student3(String name, int age) {
        this.name = name;
        this.age = age;

    }

    @Override
    public String toString() {
        return "Student3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {//这里必须抛出这个异常
        return super.clone();
    }
}

public class ObjectDemo3 {
    public static void main(String[] args) throws Exception {//这里也是必须抛出这个异常
        Student3 student3 = new Student3("xiaohonghua",18);
        System.out.println(student3);
        Object o=student3.clone(); //拷贝
        System.out.println(o);
        System.out.println(student3==o);
    }
}
/*
Student3{name='xiaohonghua', age=18}
Student3{name='xiaohonghua', age=18}
false
 */

4.Cloneable中什么成员都没有,这样的接口,在java中称之为标记接口,,只有实现了Clonable的类所产生的对象,才可以进行克隆

5.java里面的拷贝是浅拷贝

package com.shujia.day10;
//class Demo {
//    int a = 20;
//}
class Demo1{
    int a=20;
}

class Student3 implements Cloneable {
    String name;
    int age;
    Demo1 demo1;
//    Demo demo;

    public Student3() {
    }

    public Student3(String name, int age,Demo1 demo1) {
        this.name = name;
        this.age = age;
        this.demo1=demo1;

    }

    @Override
    public String toString() {
        return "Student3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {//这里必须抛出这个异常
        return super.clone();
    }
}

public class ObjectDemo3 {
    public static void main(String[] args) throws Exception {//这里也是必须抛出这个异常
        Demo1 demo1 = new Demo1();
        Student3 student3 = new Student3("xiaohonghua",18,demo1);
        System.out.println(student3.demo1);
        Object o=student3.clone(); //拷贝
        
        Student3 s=(Student3) o;
        System.out.println(s.demo1);
    }
}
/*
com.shujia.day10.Demo1@4554617c
com.shujia.day10.Demo1@4554617c
 */

二 Scanner类

2.1scanner类的概述

JDK5以后用于获取用户的键盘输入

需要导包,import java.util.Scanner;

2.2构造方法

public Scanner(InputStream source)

2.3常用方法

2.3.1next() 获取字符串

1使用方法

String 变量名=类名.next()

package com.shujia.day10;

import java.util.Scanner;

public class ScannerDemo1 {
    public static void main(String[] args) {
        //创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("亲输入你的名字");
        String name=sc.next();
        System.out.println(name);
    }

}

2.3.2nextInt() 获取整数

1使用方法

String 变量名=类名.nextInt()

package com.shujia.day10;

import java.util.Scanner;

public class ScannerDemo1 {
    public static void main(String[] args) {
        //创建Scanner对象
        Scanner sc = new Scanner(System.in);
        System.out.println("亲输入你的名字");
        String name=sc.next();
        System.out.println(name);

        System.out.println("请输入你的年龄");
        int age = sc.nextInt();
        System.out.println(age);
    }

}

2.3.3nextLine() 获取字符串 可以接收换行符

1,使用方法

String 变量名=类名.nextline()

2.nextLine()与next()区别

nextLine()这个可以监测到空格

 

 2.3.4public boolean hasNextInt() 判断管道中的下一个数据是否是整数

1.使用方法

一般在if的判断语句里面

三 String

3.1String的概述

字符串是由多个字符组成的一串数据(字符序列)

字符串可以看成是字符数组

3.2String的特点

1.一旦被创建,就不能被改变(不能改变的是其地址值)

2、Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。

3、因为String对象是不可变的,它们可以被共享(常量池)

4、字符串可以被看作一个字符数组

3.3构造方法

3.3.1 public String()

 3.3.2 public String(byte[] bytes)

3.3.3public String(byte[] bytes,int offset,int length)

1.传入的是数组

2.int offset是索引

3.int length取的是长度

3.3.4public String(char[] value) 将字符数组转成字符串

3.3.4public String(char[] value,int index,int count) 将字符数组的一部分转成字符串

3.3.5 public String(String original)

这个字符串在堆内存中

3.4String类的判断功能

3.4.1 boolean equals(Object obj) 判断两个字符串内容是否相等

3.4.2 boolean equalsIgnoreCase(String str)判断两个字符串内容是否相同,忽略大小写比较

 3.4.3 boolean contains(String str) 判断一个字符串中是否包含另一个字符串

3.4.4 boolean startsWith(String str) 判断字符串是否以某个字符串开头

 3.4.5boolean endsWith(String str) 判断字符串是否以某个字符串结尾

 3.4.6boolean isEmpty() 判断字符串是否为空字符串

 3.5 String类的获取功能

3.5.1 int length()获取字符串中的字符个数

3.5.2char charAt(int index)根据索引获取索引对应的字符

如果给出超出索引值的话会报这个错误 StringIndexOutOfBoundsException

3.5.3 int indexOf(int ch) 获取字符的位置

1.101--ASCII码:e

2.判断'e'第一次出现在字符串中的位置索引

3.如果超出ascll码则输出-1

3.5.4int indexOf(String str) 获取小字符串在大字符串中第一次出现的位置,返回小字符串第一个字符的位置索引

 3.5.5int indexOf(int ch,int fromIndex) //从fromIndex位置开始找,如果找到返回该字符在大字符串中的位置索引

1.传入的都是ascll码对应的数值

2.第一个传入的是你想找的字母,第二个传入的是从那个位置开始找,如果找到了,返回的是该字母在字符串的索引位置

3.5.6 int indexOf(String str,int fromIndex)从fromIndex位置开始找,如果找到返回该字符在大字符串中的位置索引

3.5.6String substring(int start) 字符串截取 从start索引位置截取,一直到末尾

 

 3.5.7 String substring(int start,int end) 子串开始于指定beginIndex并延伸到字符索引 endIndex-1

1.左闭右开

 3.6String类的转换功能

3.6.1 byte[] getBytes() 将字符串转换成字节数组

1.直接打印,打印的是数组的地址值

2.遍历数组得到的是相对应的ascll码值

 3.6.2char[] toCharArray() 将字符串转换成字符数组

 3.6.3 static String valueOf(char[] chs) 将字符数组转成字符串

1.这种静态方法,不需要字符串对象,直接调用

2.跟原先已有的字符串地址值不一样了11

 3.6.4static String valueOf(int i) 将数值转成字符串 

1.这种静态方法,不需要字符串对象,直接调用

2.例如 100-->"100"

 3.6.5String toLowerCase() 转小写

 3.6.6String toUpperCase() 转大写

 3.6.7 String concat(String str) 字符串拼接

 3.7 替换功能

3.7.1String replace(char old,char new) 使用新字符替换旧字符

3.7.2String replace(String old,String new) 使用字符串替换旧字符串

 3.7.3String trim() 去除字符串两边的空格

3.7.4 比较两个字符串内容是否相同 int compareTo(String str)

1.在java中比较2个字符串是否相等有两种方法,一是equals,二就是compareTo

2.equals返回的是Boolean类型,compareTo返回的是int类型

3.compare的原码

    public int compareTo(String anotherString) {
        int len1 = value.length;  //这里的value就是this了,比较的第一个字符串
        int len2 = anotherString.value.length;  //这里是比较的第二个字符串
        int lim = Math.min(len1, len2);
        char v1[] = value;  //返回字符数组
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

4.如果比较的是完全不一样的字符串就按照上面原码的判断就是运算,最后是就是第一个字符的ascll码相减。如果两个字符串前面有相同的字母,那么最后结果就是从不同的那个字符比较ascll码。如果字符是包含关系,那么就是2个字符长度的差值。

3.7.5int compareToIgnoreCase(String str) 忽略大小写

 3.8String的经典例题

3.8.1把数组中的数据按照指定个格式拼接成一个字符串

例如  int[] arr = {1,2,3};    输出结果:[1, 2, 3]

package com.shujia.day10;
/*
把数组中的数据按照指定个格式拼接成一个字符串
举例:int[] arr = {1,2,3};	输出结果:[1, 2, 3]

 */

public class StringText1 {
    public static void main(String[] args) {
        int[] arr ={1,2,3};
        //定义初始字符串
        String res1= "[";
        for (int i=0;i<arr.length;i++){
            //判断是不是最后一个字符
            if (i==arr.length-1){
                res1=res1.concat(arr[i]+"]");
            }else {
                res1=res1.concat(arr[i]+",");
            }
        }
        System.out.println(res1);
    }
}

3.8.2 字符串反转

例如 键盘录入”abc”        输出结果:”cba”

package com.shujia.day10;

public class StringText2 {
    public static void main(String[] args) {
        String res1="abc";
        String res="";
        //字符串拼接
        //遍历字符串 倒叙
        for (int i=res1.length()-1;i>=0;i--){
            res=res.concat(String.valueOf(res1.charAt(i)));
            //res1.charAt(i)遍历每个字符
            //String.valueOf(res1.charAt(i)) 将每个字符转化成字符串
        }
        System.out.println(res);

    }
}

3.8.3 统计大串中小串出现的次数

举例:在字符串” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次

package com.shujia.day10;
/*
举例:Java在字符串
” woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次

 */
public class StringText3 {
    public static void main(String[] args) {
        String bigStr="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String smallStr="java";
        int count=0;

        //判断小字符串在大字符串出现的索引位置
        int i=bigStr.indexOf(smallStr);
        //建立循环,如果小字符串不在大字符串则返回-1,如果毕设-1进入循环
        while (i!=-1){
            count++;
            //截取第一次出现小字符串的索引位置加上小字符串的长度
            bigStr=bigStr.substring(i+smallStr.length());
            //再找小字符串在截取后的大字符串的索引位置
            i=bigStr.indexOf(smallStr);
        }
        System.out.println(count);


    }
}

3.9经典问题

1.String s = new String(“hello”)和String s = “hello”;的区别?

String s = new String(“hello”) 这个是new出来一个字符串对象,他的地址值在堆内存中

String s = “hello” 这个的地址值是指向常量值地址值

四StringBuffer类

4.1StringBuffer的概述

1.我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题

2.线程安全的可变字符序列

4.2构造方法

4.2.1public StringBuffer() 创建一个空的StringBuffer容器

1.查看容器容量 public int capacity() 返回当前容量。初始容量是16

2.返回容器里面的内容长度public int length() 返回长度(字符数)

3.StringBuffer重写了toString()

4.2.2public StringBuffer(int capacity) 创建指定大小容量的容器

4.2.3public StringBuffer(String str) 创建StringBuffer的同时,内部就包含了一个初始字符串

1.给定字符串的话,容器的容量就是16+字符串的长度

2.里面的内容也就是字符串的长度

4.3成员方法

4.3.1public StringBuffer append(String str) 追加

4.3.2 public StringBuffer insert(int index,String str) 在容器中指定位置添加字符串

 4.3.3public StringBuffer deleteCharAt(int index) 删除指定位置索引上的字符

4.3.4 public StringBuffer delete(int start,int end) [start,end)

4.3.5 public StringBuffer replace(int start,int end,String str) [start,end)

4.3.6public StringBuffer reverse() 反转

 

4.3.7public String substring(int start) 截取

1.截取本身容器不会发生改变

2.这里的int是从索引开始一直截到最后

4.3.8public String substring(int start,int end)

 4.4String与StringBuffer之间的转化

4.4.1String-->StringBuffer

使用StringBuffer里面的构造方法

4.4.2StringBuffer-->String

1.toString方法

2.截取方法

4.5StringBuffer,StringBuilder的区别

1.StringBuffer是线程安全的

2.StringBuilder线程是不安全的

4.6StringBuffer与String传参的区别

4.6.1.StringBuffer传参

package com.shujia.day11;

public class StringBufferDemo2 {
    public static void main(String[] args) {

        StringBuffer sb1 = new StringBuffer("hello");
        StringBuffer sb2 = new StringBuffer("world");
        System.out.println(sb1); //hello
        System.out.println(sb2);//world
        change(sb1,sb2);
        System.out.println(sb1);//hello  //方法调用完main函数里面的sb1的地址值没有变,还是指向堆内存中的sb1
        System.out.println(sb2);//worldworld  //方法调用完main函数里面的sb2的地址值没有变,还是指向堆内存中的sb2,但是堆内存是sb2的内容变了


    }
    public static void change(StringBuffer sb1,StringBuffer sb2){
        sb1=sb2;//sb2的地址值赋值给sb1
        sb2=sb1.append(sb2); //这里sb2里面多了一个world
        System.out.println(sb1);//worldworld 原因是打印的是sb1在堆内存的结果,而sb1的地址值是sb2了,sb2的内容是这个
        System.out.println(sb2);//worldworld  打印的sb2在堆内存的结果
    }
}

 4.6.2String传参

1.创建了字符串对象,里面的内容就不会变

package com.shujia.day11;

public class StringBufferDemo2 {
    public static void main(String[] args) {
        String s1="hello";
        String s2="world";
        System.out.println(s1);//hello
        System.out.println(s2);//world
        change(s1,s2);
        System.out.println(s1);//hello
        System.out.println(s2);//world
        
    }
    public static void change(String s1,String s2){
        s1=s2;
        s2=s1+s2;
        System.out.println(s1);//world
        System.out.println(s2);//worldworld
    }

}

五Arrays类

5.1概述

1.针对数组进行操作的工具类。

2.提供了排序,查找等功能

5.2成员方法

1.静态直接用2.直接Arrays.方法名

5.2.1public static String toString(int[] a) 将数组以字符串的形式返回

1.以后我们想把数组变成字符串输出就不需要再遍历数组然后评判是不是最后一位了,直接用这个方法


import java.util.Arrays;

public class ArraysDemo1 {
    public static void main(String[] args) {
        int[] arr={11,22,33,44,55,66};
        String s1 = Arrays.toString(arr);
        System.out.println(s1); //[11, 22, 33, 44, 55, 66]
    }
}

5.2.2 public static void sort(int[] a) 快速排序,结果是升序的

1.排序直接Arrays.sort(数组)

5.2.3public static int binarySearch(int[] a,int key) 二分查找 返回查找到元素的索引

1.数组必须是有序的

2.如果查找的在数组里面则返回索引值,若不在则去看原码

原码如下

 a就是给定的数组,key就是查询的元素

fromIndex就是0

toIndex就是数组的长度

 

 六包装类

6.1概述

1.将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。

2.基本类型和包装类的对应 Byte,Short,Integer,Long,Float,Double    Character,Boolean

6.2Integer类

6.2.1Integer概述

1.Integer 类在对象中包装了一个基本类型 int 的值

2.该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法

6.2.2构造方法

1.public Integer(int value) 将基本数据类型int包装成引用数据类型Integer

1.Integer类里面重写了toString方法

2.定义直接 Integer 类名 =整数;此时自动装箱

2.打印这个类名时自动拆箱

package com.shujia.day11;

public class Integer1 {
    public static void main(String[] args) {
        int i1 =10;
        Integer i2 = new Integer(100);
        System.out.println(i2);//这样发现有警告,所以直接这样写
        Integer i3=50;
        System.out.println(i3);//50
        //Integer里面重写了toString方法

    }
}
2.public Integer(String s)传一个数字类型的字符串

 6.2.3成员方法:

1.public int intValue() 手动获取数值

 2.public static int parseInt(String s) 将数字型的字符串转成数字

 3.public static String toString(int i)将int转化为字符串

 4.public static Integer valueOf(int i) int-->Integer

5.public static Integer valueOf(String s)  String-->Integer

 6常用的进制转化

1.public static String toBinaryString(int i) 数字转化为二进制的字符串类型

2.public static String toOctalString(int i)  数字转化为八进制的字符串类型

3.public static String toHexString(int i)  数字转化为十六进制的字符串类型

4.public static String toString(int i,int radix) 十进制转化为其他进制,

5.public static int parseInt(String s,int radix)其他进制转为十进制

6.3Character

6.3.1概述

1.Character 类在对象中包装一个基本类型 char 的值

2.此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然

6.3.2构造方法

1.public Character(char value)

6.3.3成员方法

1.public static boolean isUpperCase(char ch) 判断字符是否为大写

2.public static boolean isLowerCase(char ch) 判断字符是否为小写

3.public static boolean isDigit(char ch) 判断字符是否为数字

4.public static char toUpperCase(char ch) 转大写

5.public static char toLowerCase(char ch) 转小写

 七Random类

7.1概述

1.此类用于产生随机数

2.如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

7.2构造方法

7.2.1public Random()

7.3成员方法

7.3.1public int nextInt()随机生成int类型

7.3.2public int nextInt(int n)  [0,n)

八System

8.1概述

1.System 类包含一些有用的类字段和方法。它不能被实例化。

2.是一个工具类

8.2成员方法

8.2.1.public static void gc() 垃圾回收

8.2.2public static void exit(int status) 强制退出

package com.shujia.day11;

public class SystemDemo1 {
    public static void main(String[] args) {
        for (int i=0;i<10;i++){
            if (i==5){
//                break;
                System.exit(0);
            }
            System.out.println(i);
        }
        System.out.println("hello");
    }
}
/*
break的输出结果 1 2 3 4 hello
System.exit(0) 的输出结果 1 2 3 4

 */

8.2.3public static long currentTimeMillis() 获取一个时间戳

九Date

9.1概述

1.类 Date 表示特定的瞬间,精确到毫秒。

9.2构造方法

9.2.1public Date() 将程序运行到此行时,此刻的时间

9.2.2public Date(long date) 将指定的时间戳转成对应的时间

十DateFormat类

10.1概述

1.DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。

2.是抽象类,所以使用其子类SimpleDateFormat

10.2SimpleDateFormat 格式化时间

10.2.1public SimpleDateFormat(String pattern) yyyy年MM月dd日 HH时mm分ss秒

package com.shujia.day11;

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

public class SimpleDataFormatDemo1 {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月DD日 HH时mm分ss秒");

        Date date = new Date();
        String str = sdf.format(date);
        System.out.println(str);//2024年03月71日 19时38分38秒

    }
}

10.3小练习

10.3.1.时间戳转指定类型的时间

1.先转化为DATa类型

2.再格式化

package com.shujia.day11;

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

public class SimpleDataFormatDemo1 {
    public static void main(String[] args) {
        Date date = new Date(1710156117243l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月DD日 HH时mm分ss秒");
        String str = sdf.format(date);
        System.out.println(str);//2024年03月71日 19时21分57秒

    }
}

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值