java常用的API
【摘要】
java常用的API
文章目录
java常用的API一、Object类1.toString()方法,equals()方法2.String中的equals方法
二、system类三、Str...
java常用的API
一、Object类
1.toString()方法,equals()方法
toString()方法,返回当前对象在堆内存中的地址。
重点:子类重写后,返回子类对象的内容
案例:
package com.object;
public class ToStringTest {
public static void main(String[] args) {
//toString()方法,返回当前对象在堆内存中的地址。
Student s=new Student("小马哥",23);
String s1 = s.toString();
System.out.println(s1);
System.out.println(s);
/*
com.object.ToStringTest$Student@1b6d3586
com.object.ToStringTest$Student@1b6d3586
*/
/*
子类重写之后
Student{name='小马哥', age=23}
Student{name='小马哥', age=23}
*/
//equals()方法
Student stu1=new Student("小飞侠",44);
Student stu2=new Student("小飞侠",44);
System.out.println(stu1.equals(stu2));
System.out.println(stu1==stu2);
/*
false
false
*/
}
public static class Student{
String name;
int age;
public Student(String name) {
this.name = name;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//让子类重写,以便返回子类对象的内容
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
2.String中的equals方法
package com.object;
public class equalsTest {
public static void main(String[] args) {
String s1="小飞侠";
String s2="小飞侠";
System.out.println(s1.equals(s2)); //true
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
二、system类
1.exit()方法,jvm终止。
2.currentTimeMillis()放法,从1970-1-1 00:00:00 到此刻的毫秒值
案例:
package com.system;
public class Test1 {
public static void main(String[] args) {
System.out.println("程序开始!");
//System.exit(0); //jvm终止
//1970-1-1 00:00:00 到此刻的毫秒值
System.out.println(System.currentTimeMillis());
//进行时间的计算和系统分析
//执行前
long startTime=System.currentTimeMillis();
for (int i = 0; i <999999 ; i++) {
System.out.println(i);
}
//执行后
long endTime=System.currentTimeMillis();
System.out.println((endTime-startTime)/1000+"秒"); //4秒
System.out.println("程序终止!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
三、StringBuilder类
package com.stringBuilder;
public class Test1 {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
sb.append("小");
sb.append("飞");
sb.append("侠");
sb.append("R");
sb.append("C");
System.out.println(sb); //小飞侠RC
//链式编程
StringBuilder sb1=new StringBuilder();
sb1.append("小").append("飞").append("侠");
System.out.println(sb1); //小飞侠
//反转
sb1.reverse();
System.out.println(sb1); //侠飞小
//将拼接的字符串转换为String类型
StringBuilder sb2=new StringBuilder();
sb2.append("马奎斯").append("RC").append("213").append("V");
String s = sb2.toString();
System.out.println(s);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
四、Math类
package com.math;
public class test1 {
public static void main(String[] args) {
//向上取整
System.out.println(Math.ceil(5.2)); //6.0
//向下取整
System.out.println(Math.floor(5.2)); //5.0
//绝对值
System.out.println(Math.abs(5)); //5
System.out.println(Math.abs(-5)); //5
//四舍五入
System.out.println(Math.round(3.14156)); //3
//指数
System.out.println(Math.pow(5,2)); //25.0
//随机数
System.out.println(Math.random()); //范围0-1 0.20888027158105127
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
五、Date类
1.data类的创建
package com.date;
import java.util.Date;
public class Test {
public static void main(String[] args) {
//创建一个date类对象,当前系统时间
Date d=new Date();
System.out.println(d); //Thu Jun 02 11:01:46 CST 2022
//1小时2分100秒之后
long time=System.currentTimeMillis();
time+=(62*60+100)*1000;
Date d1=new Date(time);
System.out.println(d1);
/*
Thu Jun 02 11:09:08 CST 2022
Thu Jun 02 12:12:48 CST 2022
*/
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2,格式化日期
package com.date;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test1 {
public static void main(String[] args) {
//系统当前日期
Date d=new Date();
System.out.println(d);
//格式化日期
SimpleDateFormat sd=new SimpleDateFormat("YYYY-MM-DD HH:mm:ss EEE a");
String data = sd.format(d);
System.out.println(data);
/*
Thu Jun 02 11:20:10 CST 2022
2022-06-153 11:20:10 星期四 上午
*/
//40秒之后的时间
long time = System.currentTimeMillis()+40*1000;
String data1 = sd.format(time);
System.out.println(data1);
/*
2022-06-153 11:24:58 星期四 上午
2022-06-153 11:25:38 星期四 上午
*/
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
3.字符串时间解析
package com.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//字符串时间解析
public class Test2 {
public static void main(String[] args) throws ParseException {
//例如当前时间为2022-6-2 12:12:12 往后退5天5小时5分钟5秒,时间是多少
String currentTime="2022-6-2 12:12:12";
//格式化日期
SimpleDateFormat sd=new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
//把字符串时间解析成日期对象
Date parse = sd.parse(currentTime);
//最终毫秒值
long time1=parse.getTime()+(5L*24*60*60+5*60*60+5*60+5)*1000;
//转化为规定的格式,输出
System.out.println(sd.format(time1)); //2022-12-365 17:17:17
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
六、JDK8新增日期API
package com.NewDate;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.MonthDay;
public class Test {
public static void main(String[] args) {
//获取本地日期对象
LocalDate ld=LocalDate.now();
System.out.println("今天日期是:"+ld); //今天日期是:2022-06-02
int year = ld.getYear();
System.out.println("year="+year); //year=2022
int month = ld.getMonthValue();
System.out.println("month="+month); //month=6
int day = ld.getDayOfMonth();
System.out.println("day="+day); //day=2
//当年的第几天
int dayOfYear = ld.getDayOfYear();
System.out.println(dayOfYear); //153
//星期
DayOfWeek dayOfWeek = ld.getDayOfWeek();
System.out.println(dayOfWeek.getValue()); //4
//自定义日期
LocalDate of = LocalDate.of(2000, 5, 2);
System.out.println(of); //2000-05-02
//判断自定义日期是在当前时间的前面还是后面
//判断自定义日期是在当前时间的前面
System.out.println(of.isBefore(ld)); //true
//判断自定义日期是在当前时间的后面
System.out.println(of.isAfter(ld)); //false
//判断今天是你的生日吗
LocalDate birthDay = LocalDate.of(2000, 5, 2);
LocalDate ld1=LocalDate.now();
MonthDay of1 = MonthDay.of(birthDay.getMonthValue(), birthDay.getDayOfMonth());
MonthDay of2 = MonthDay.from(ld1);
System.out.println(of1.equals(of2)); //false
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
package com.NewDate;
import java.time.LocalTime;
public class Test1 {
public static void main(String[] args) {
//获取本地时间
LocalTime lt=LocalTime.now();
System.out.println(lt); //13:33:37.858
//时
int hour = lt.getHour();
System.out.println("hour="+hour); //hour=13
//分
int minute = lt.getMinute();
System.out.println("minute="+minute); //minute=33
//秒
int second = lt.getSecond();
System.out.println("second="+second); //second=37
//纳秒
int nano = lt.getNano();
System.out.println("nano="+nano); //nano=858000000
//自定义时间
LocalTime of = LocalTime.of(12, 12, 12);
System.out.println(of); //12:12:12
LocalTime lt1=LocalTime.now();
System.out.println("当前时间:"+lt1);
//一小时前
System.out.println(lt1.minusHours(1));
//一分钟前
System.out.println(lt1.minusMinutes(1));
//一秒前
System.out.println(lt1.minusSeconds(1));
//一纳秒前
System.out.println(lt1.minusNanos(1));
//一小时后
System.out.println(lt1.plusHours(1));
//一分钟后
System.out.println(lt1.plusMinutes(1));
//一秒后
System.out.println(lt1.plusSeconds(1));
//一纳秒后
System.out.println(lt1.plusNanos(1));
/*
当前时间:13:47:30.235
12:47:30.235
13:46:30.235
13:47:29.235
13:47:30.234999999
14:47:30.235
13:48:30.235
13:47:31.235
13:47:30.235000001
*/
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
package com.NewDate;
import java.time.LocalDateTime;
public class Test2 {
public static void main(String[] args) {
//日期 时间
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt); //2022-06-02T13:41:20.113
//年
System.out.println(ldt.getYear()); //2022
//月
System.out.println(ldt.getMonthValue()); //6
//日
System.out.println(ldt.getDayOfMonth()); //2
//时
System.out.println(ldt.getHour()); //13
//分
System.out.println(ldt.getMinute()); //41
//秒
System.out.println(ldt.getSecond()); //20
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
package com.NewDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test3 {
public static void main(String[] args) throws Exception{
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt); //2022-06-02T14:25:09.414
//格式化器
//正向格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(dateTimeFormatter.format(ldt)); //2022-06-02 02:25:09
//逆向格式化
System.out.println(ldt.format(dateTimeFormatter)); //2022-06-02 02:25:09
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43514330/article/details/125098259
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)