美文网首页
toString 和Date

toString 和Date

作者: 山不转人自转 | 来源:发表于2020-10-19 10:50 被阅读0次

toString():返回该对象的字符串表示。
如果类需要输出或返回或打印字符串类型可以重写同String方法,或者按Alt加回车找到toString方法默认重写toString方法。

equals(Object obj) :指示其他某个对象是否与此对象“相等”。

Date日期类:

//通过这个方法格式化日期
y=年,M=月,d=日,H=小时,m=分,s=秒
SimpleDateFormat sdf=new SimpleDateFormat (yyyy-MM-dd);//表示转话的格式为2020-10-18

sdf.applyPattern():也可以实现类型转换
format(Date date):把date类转换为字符串类;
parse(String str):把字符串类型转换为Date类型;

Date 的 getTime()方法是获取当前时间的毫秒数;

Calendar 日历类

Calendar cal = Calendar.getInstance();//获取当前默认时区的日历

YEAR  年,MONTH 月,DAY_OF_MONTH 日,HOUR  (12小时)
HOUR_OF_DAY (24小时),MINUTE  分,SECOND  秒
DAY_OF_WEEK-1 礼拜几-1是因为从周日开始。

get():用来获取日历里的年月日之类的数据如 int year = cal.get(Calendar.YEAR);
set():用来设置指定字段的值如           cal.set(Calendar.YEAR, 2022); 
类的set方法返回类型都是void();
日历类中的getTime方法:是返回Date对象,而不是毫秒数

System类

currentTimeMillis():获取毫秒数//可以用来检测一段代码运行时长。

StringBuilder类字符串缓冲区

append(...) :添加任意类型数据的字符串形式,并返回当前对象自身。
toString() :将当前StringBuilder对象转换为String对象。

包装类转换成基本数据类型的方法

public static byte parseByte(String s) :将字符串参数转换为对应的byte基本类型。
public static short parseShort(String s) :将字符串参数转换为对应的short基本类型。
public static int parseInt(String s) :将字符串参数转换为对应的int基本类型。
public static long parseLong(String s) :将字符串参数转换为对应的long基本类型。
public static float parseFloat(String s) :将字符串参数转换为对应的float基本类型。
public static double parseDouble(String s) :将字符串参数转换为对应的double基本类型。
public static boolean parseBoolean(String s) :将字符串参数转换为对应的boolean基本类型。

包装类

byte => Byte
short => Short
int => Integer
long => Long
float => Float
double => Double
char => Character
boolean => Boolean

自动装箱就是将基本数据类型自动转换为包装类
Integer i=300;
自动拆箱就是包装类自动转换为基本数据类型
int a=i+5;

相关文章

网友评论

      本文标题:toString 和Date

      本文链接:https://www.haomeiwen.com/subject/uujhmktx.html