JavaSE常用API
1、Math.round(11.5)等于多少?Math.round(-11.5)等于多少?
Math.round(11.5) = 12
Math.round(-11.5) = -11
2、switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?
Java5以前,switch(expr)中,expr只能是byte、short、char、int。从Java5开始,Java中引入了枚举类型,expr也可以是enum类型。
Java7开始,expr还可以是字符串(String),但是长整型(long)在目前的所有版本中都是不可以的
3、数组有没有length()方法?String有没有length()方法?
数组没有length()方法,而是length属性,String有length()方法,在JavaScript中,获取字符串的长度是通过length属性得到的,这一点容易和Java混淆。
4、String、StringBuilder、StringBuffer的区别?
Java平台提供了两种类型的字符串:String和StringBuffer/StringBuilder,它们都可以存储和操作字符串,区别如下:
1)String是只读字符串,也就意味着String引用的字符串内容是不能被改变的,初学者可能会有这样的误解:
String str = “abc”;
Str = “bce”;
Str本质上是一个引用对象,它只向一个字符串对象”abc”,第二行代码的意义是让str重新指向了一个新的字符串“bcd”对象,而“abc”对象没有任何改变。
2)StringBuffer/StringBuilder 表示的字符串对象是个一直接进行修改的
3)StringBuilder是Java5中引入的,它和StringBuffer的方法完全相同,区别在于它是在单线程环境下使用的,因为它的所有方法都没有做同步处理,因此它的效率理论上也比StringBuffer要高
5、什么情况下使用“+”运算符进行字符串连接比调用StringBuffer/StringBuilder对象的Append方法连接字符串性能更好?(华为)
字符串是Java程序中最常用的数据结构之一,在Java中String类已经重载了“+”,也就是说,字符串可以直接使用“+”进行连接
String s = “abc” + “bcd”;
在Java中提供了一个StringBuilder类,这个类也可以起到“+”的作用
通过字节码反编译可知,虽然在源程序中使用了“+”,但是编译时仍然将“+”转换成了StringBuilder 因此我们可以得出结论,在Java中无论使用何种方式进行字符串连接,实际上都使用的是StringBuilder 那么是不是可以根据这个结论推出使用“+”和StringBuilder的效果是一样的那?这个要从两方面来解释,如果从运行结果来解释,那么“+”和StringBuilder是完全等效的,但是如果从运行效率和资源消耗来看,那么它们将存在很大的区别。
当然,如果连接字符串表达式很简单,那么“+”和StringBuilder基本上是一样的,但如果结构比较复杂,如使用循环来连接字符串,那么产生的Java Byte Code就会有很大的区别
虽然编译器将“+”转换成了StringBuilder,但创建StringBuilder对象的位置却在for语句内部,这就意味着每执行一次循环,就会创建一个StringBuilder对象,虽然Java有垃圾回收器,但是这个回收器的工作时间是不定的,如果不断的产生这样的垃圾,那么仍然会占用大量的资源,解决这个问题的方法就是在程序中直接使用StringBuilder来连接字符串,在使用StringBuilder时要注意,尽量不要难过“+”和StringBuiler混着用,否则会产生更多的StringBuilder对象
6、String的intern()方法?
String对象的intern()方法会得到字符串对象在常量池中对应的版本的引用,如果常量池中有一个字符串与String对象的equals结果时true,如果常量池中没有对应的字符串,则该字符串将被添加到常量池中,然后返回常量池中字符串的引用;
7、Java中的日期和时间
1)如何获取年月日、时分秒?
Calendar cal = Calendar.getInstance();
年:cal.get(Calendar.YEAE);
月:cal.get(Calendar.MONTH); // 0 - 11
日:cal.get(Calendar.DATE);
时:cal.get(Calendar.HOUR_OF_DAY);
分:cal.get(Calendar.MINUTE);
秒:cal.get(Calendar.SECOND);
// Java 8
LocalDateTime dt = LocalDateTime.now();
年:dt.getYear();
月:dt.getMonthValue(); // 1 - 12
日:dt.getDayOfMonth();
时:dt.getHour();
分:dt.getMinute();
秒:dt.getSecond();
2)如何获取从1970/1/1 00:00:00到现在的毫秒数?
1:Calendar.getInstance().getTimeInMillis();
2:System.currentTimeMillis();
3:Clock.systemDefaultZone().millis(); // Java 8
3)如何取得某月的最后一天?
// 获取当前月的第一天
Calendar c = Calendar.getInstance();
c.add(Calendar.Month,0);
c.set(Calendar.DAY_OF_MONTH,1); // 设置为1号,当前日期即为本月第一天
// 获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
// Java 8
LocalDate today = LocalDate.now();
// 本月的第一天
LocalDate firstDay = LocalDate.of(today.getYear(),today.getMonth(),1);
// 本月最后一天
LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
4)如何格式化日期?
Java.text.DataFormat 的子类,如SimpleDateFormat类中的,format(Date)方法可以将日期格式化
Java8中可以用java.time.format.DateTimerFormatter来格式化日期
SimpleDateFormat oldFormatter = new SimpleDateFormat(“yyyy/MM/dd”);
Date date = new Date();
oldFormatter.format(date);
// Java 8
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern(“yyyy/MM/dd”);
LocalDate date = LocalDate.now();
date.format(newFormatter));
Java的日期API一直以来都是被诟病的东西,为了解决这一问题,Java8引入的新的时期API,其中包括LocalDate、LocalTime、LocalDateTime、Clock、Instant等类,这些类的设计都使用了不变模式,因此是线程安全的设计。
5)打印昨天的当前时刻?
Calender cal = Calendar.getInstance();
cal.add(Calendar.DATE,-1);
// Java 8
LocalDateTime today = LocalDateTime.now();
LocalDateTime yesterday = today.minusDay(1);
6) Java 8 的时间特性?
java 8日期/时间API是JSR-310的实现,它的实现目标是克服旧的日期时间实现中所有的缺陷,新的日期/时间API的一些设计原则是:
不变性:新的日期/时间API中,所有的类都是不可变的,这对多线程环境有好处。
关注点分离:新的API将人可读的日期时间和机器时间明确分离,它为日期(Date)、时间(Time)、日期时间(DateTime)、时间戳(unix timestame)已及时区定义了不同的类。
清晰:在所有的类中,方法都被明确定义用以完成相同的行为,举个例子,要拿到当前实例我们可以使用now()方法,在所有的类中都定义了format()和parse()方法,而不是像以前那样专门有一个独立的类,为了更好的处理问题,所有的类都使用了工厂模式和策略模式,一旦你使用了其中某个类的方法,与其他类协同工作并不困难。
实用操作:所有新的日期/时间API类都实现了以系列方法用以完成通用的任务,如加、减、格式化、解析、从日期/时间中提取单独部分,等等。
可扩展性:新的日期/时间API是工作在ISO-8601日历系统上的,但我们也可以将其应用在非ISO的日历上。
Java 8 日期/时间API包解释
java.time包:这是新的Java日期/时间API的基础包,所有的主要基础类都是这个包的一部分,如LocalDate,LocalTime,LocalDateTime,Instant,Period,Duration等等,所有这些类都是不可变和线程安全的,在绝大数情况下,这些类能够有效的处理一些公共的需求。
java.time.chrono包:这个包为非ISO的日历系统定义了一些泛化的API,我们可以扩展AbstractChronology类创建自己的日历系统。
java.time.format包:这个包包含能够格式化和解析日期时间对象的类,在绝大多数情况下,我们不应该直接使用它们,因为java.time包中相应的类已经提供了格式化和解析的方法。
java.time.temporal包:这个包包含了一些时态对象,我们可以用其找出关于日期/时间对象的某个特定日期或时间,比如可以找到某月的第一天或者最后一天,你可以非常容易的认出这些方法,因为它们都具有“withXXX”的格式
java.time.zone包:这个包包含支持不同时区以及相关规则的类
Java 8 日期/时间常用API
java.time.LocalDate
LocalDate是一个不可变的类,它表示默认格式(yyyy-MM-dd)的日期,我们可以使用now()方法得到当前时间,也可以提供输入年份、月份和日期的输入参数来创建一个LocalDate实例,该类为now()方法提供了重载方法,我们可以传入ZoneId来获取指定时区的日期。该类提供与Java.sql.Date相同的功能,对于如何使用该类,例子如下:
// Current Date
LocalDate today = LocalDate.now();
// Creating LocalDate by providing input arguments
LocalDate firstDay_2014 = LocalDate.of(2014,Month.JANUARY,1);
// Current date in”Asia/Kolkata”, you can get it from zoneID javadoc
LocalDate todayKolkata = LocalDate.now(ZoneId.of(“Asia/Kolkata”);
// Getting date from the base date i.e 01/01/1970
LocalDate dateFromBase = LocalDate.ofEpochDay(365);
LocalDate hundredDay2014 = LocalDate.ofYearDay(2014,100);
Java.time.LocalTime
LocalTime是一个不可变类,它的实例代表一个符合人类可读格式的时间,默认格式是hh:mm:ss,zzz 像LocalDate一样,该类也提供了时区支持,同时也可以传入小时,分钟和秒等输入参数创建实例,我们来看一个简单的示例:
// Current Time
LocalTime time = LocalTime.now();
// Creating LocalTime by providing input arguments
LocalTime specificTime = LocalTime.of(12,20,25,40);
java.time.LocalDateTime
LocalDateTime 是一个不可变的日期/时间对象,它表示一组日期/时间,默认格式是yyyy-MM-dd-HH-mm-ss.zzz 它提供了一个工厂方法,接收LocalDate和LocalTime输入参数,创建LocalDateTime实例
java.time.Instant
Instant类是用在机器可读的时间格式的,它以Unix时间戳的形式存储日期时间,我们来看一个简单的程序
日期API工具
我们早些时候提到过,大多数日期/时间API类都实现了一系列工具方法,如加/减天数、周数、月份数等,还有其他的工具方法能够使用TemporalAdjuster调整日期,并计算两个日期间的周期
LocalDate today = LocalDate.now();
// Get the Year, check if it's leap year
System.out.println("Year "+today.getYear()+" is Leap Year? "+today.isLeapYear());
// Compare two LocalDate for before and after
//Create LocalDateTime from LocalDate
System.out.println("Current Time="+today.atTime(LocalTime.now()));
//plus and minus operations
System.out.println("10 days after today will be "+today.plusDays(10));
System.out.println("3 weeks after today will be "+today.plusWeeks(3));
System.out.println("20 months after today will be "+today.plusMonths(20));
System.out.println("Today is before 01/01/2015? "+today.isBefore(LocalDate.of(2015,1,1)));
System.out.println("10 days before today will be "+today.minusDays(10));
System.out.println("3 weeks before today will be "+today.minusWeeks(3));
System.out.println("20 months before today will be "+today.minusMonths(20));
//Temporal adjusters for adjusting the dates
System.out.println("First date of this month= "+today.
with(TemporalAdjusters.firstDayOfMonth()));
LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
System.out.println("Last date of this year= "+lastDayOfYear);
Period period = today.until(lastDayOfYear);
System.out.println("Period Format= "+period);
System.out.println("Months remaining in the year= "+period.getMonths());
解析和格式化
//Format examples
LocalDate date = LocalDate.now();
//default format
System.out.println("Default format of LocalDate="+date);
//specific format
System.out.println(date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu")));
System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
LocalDateTime dateTime = LocalDateTime.now();
//default format
System.out.println("Default format of LocalDateTime="+dateTime);
//specific format
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")));
System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
Instant timestamp = Instant.now();
//default format
System.out.println("Default format of Instant="+timestamp);
//Parse examples
LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48",
DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"));
System.out.println("Default format after parsing = "+dt);
8、Java 8日期实现JSR310规范
1)JSR介绍,JSR310实际上有两个日期概念,第一个是Instant,它大致对应于java.util.Date类,因为它代表了一个确定的时间点,即相对于标准Java纪元(1970/01/01)的偏移量;但与java.util.Date类不同的是其精确到了纳秒级别
第二个对应于人类自身的观念,比如LocalDate和LocalTime。它们代表了一般的时区概念,要么是日期(不包含时间),要么是时间(不包含日期),类似于java.sql的表示方式。此外,还有一个MonthDay,它可以存储某人的生日(不包括年份)。每个类都在内部存储正确的数据而不是像java.util.Date那样利用午夜12点来区分日期,利用1970/01/01来表示时间。
目前Java 8 已经实现了JSR310的全部内容,新增了java.time包定义的类表示了日期/时间概念的规则,包括instants,durations,dates,times,times-zones and periods 这些都是基于ISO日历系统,它又是遵循Gregorian规则的,最重要的一点是值是不可变,且线程安全,通过如下一张图,可知;
Java8 方法概览
java.time 包下的方法概览
与旧的 API 相比
简单实用 java.time 的 API
Clock
// Clock 时钟提供给我们用于访问某个特定 时区的 瞬时时间、日期 和 时间的。
Clock c1 = Clock.systemUTC(); //系统默认 UTC 时钟(当前瞬时时间
System.currentTimeMillis())
System.out.println(c1.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c2 = Clock.systemDefaultZone(); //系统默认时区时钟(当前瞬时时间)
Clock c31 = Clock.system(ZoneId.of("Europe/Paris")); //巴黎时区
System.out.println(c31.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c32 = Clock.system(ZoneId.of("Asia/Shanghai"));//上海时区
System.out.println(c32.millis());//每次调用将返回当前瞬时时间(UTC)
Clock c4 = Clock.fixed(Instant.now(), ZoneId.of("Asia/Shanghai"));//固定上海时区时钟
System.out.println(c4.millis());
System.out.println(c4.millis()); //不变 即时钟时钟在那一个点不动
Clock c5 = Clock.offset(c1, Duration.ofSeconds(2));
// 相对于系统默认时钟两秒的时钟
System.out.println(c1.millis());
System.out.println(c5.millis());
Instant
// Instant 瞬时时间 相当于以前的 System.currentTimeMillis()
Instant instant1 = Instant.now();
System.out.println(instant1.getEpochSecond());//精确到秒 得到相对于 1970-01-01 00:00:00 UTC 的一个时间
System.out.println(instant1.toEpochMilli()); //精确到毫秒
Clock clock1 = Clock.systemUTC(); //获取系统 UTC 默认时钟
Instant instant2 = Instant.now(clock1);//得到时钟的瞬时时间
System.out.println(instant2.toEpochMilli());
Clock clock2 = Clock.fixed(instant1, ZoneId.systemDefault());
//固定瞬时时间时钟
Instant instant3 = Instant.now(clock2);
//得到时钟的瞬时时间
System.out.println(instant3.toEpochMilli());//equals instant1
LocalDateTime
//使用默认时区时钟瞬时时间创建 Clock.systemDefaultZone() -->即相对于 ZoneId.systemDefault() 默认时区
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//自定义时区
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);//会以相应的时区显示日期
//自定义时钟
Clock clock = Clock.system(ZoneId.of("Asia/Dhaka"));
LocalDateTime now3 = LocalDateTime.now(clock);
System.out.println(now3);//会以相应的时区显示日期
//不需要写什么相对时间 如 java.util.Date 年是相对于 1900 月是从 0 开始
//2013-12-31 23:59
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59);
//年月日 时分秒 纳秒
LocalDateTime d2 = LocalDateTime.of(2013, 12, 31, 23, 59, 59, 11);
//使用瞬时时间 + 时区
Instant instant = Instant.now();
LocalDateTime d3 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println(d3);
//解析 String--->LocalDateTime
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59");
System.out.println(d4);
LocalDateTime d5 = LocalDateTime.parse("2013-12-31T23:59:59.999");//999 毫秒 等价于 999000000 纳秒
System.out.println(d5);
//使用 DateTimeFormatter API 解析 和 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime d6 = LocalDateTime.parse("2013/12/31 23:59:59", formatter);
System.out.println(formatter.format(d6)); 65. //时间获取
System.out.println(d6.getYear());
System.out.println(d6.getMonth());
System.out.println(d6.getDayOfYear());
System.out.println(d6.getDayOfMonth());
System.out.println(d6.getDayOfWeek());
System.out.println(d6.getHour());
System.out.println(d6.getMinute());
System.out.println(d6.getSecond());
System.out.println(d6.getNano());
//时间增减
LocalDateTime d7 = d6.minusDays(1);
LocalDateTime d8 = d7.plus(1, IsoFields.QUARTER_YEARS);
//LocalDate 即年月日 无时分秒
/LocalTime 即时分秒 无年月日
//API 和 LocalDateTime 类似就不演示了
LocalDateTime
//使用默认时区时钟瞬时时间创建 Clock.systemDefaultZone() -->即相对于 ZoneId.systemDefault() 默认时区
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//自定义时区
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);//会以相应的时区显示日期
//自定义时钟
Clock clock = Clock.system(ZoneId.of("Asia/Dhaka"));
LocalDateTime now3 = LocalDateTime.now(clock);
System.out.println(now3);//会以相应的时区显示日期
//不需要写什么相对时间 如 java.util.Date 年是相对于 1900 月是从 0 开始
//2013-12-31 23:59
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59);
//年月日 时分秒 纳秒
LocalDateTime d2 = LocalDateTime.of(2013, 12, 31, 23, 59, 59, 11);
//使用瞬时时间 + 时区
Instant instant = Instant.now();
LocalDateTime d3 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println(d3);
//解析 String--->LocalDateTime
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59");
System.out.println(d4);
LocalDateTime d5 = LocalDateTime.parse("2013-12-31T23:59:59.999");//999 毫秒 等价于 999000000 纳秒
System.out.println(d5);
//使用 DateTimeFormatter API 解析 和 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime d6 = LocalDateTime.parse("2013/12/31 23:59:59", formatter);
System.out.println(formatter.format(d6)); 65. //时间获取
System.out.println(d6.getYear());
System.out.println(d6.getMonth());
System.out.println(d6.getDayOfYear());
System.out.println(d6.getDayOfMonth());
System.out.println(d6.getDayOfWeek());
System.out.println(d6.getHour());
System.out.println(d6.getMinute());
System.out.println(d6.getSecond());
System.out.println(d6.getNano());
//时间增减
LocalDateTime d7 = d6.minusDays(1);
LocalDateTime d8 = d7.plus(1, IsoFields.QUARTER_YEARS);
//LocalDate 即年月日 无时分秒
/LocalTime 即时分秒 无年月日
//API 和 LocalDateTime 类似就不演示了
ZonedDateTime
//即带有时区的 date-time 存储纳秒、时区和时差(避免与本地 date-time 歧义)。
//API 和 LocalDateTime 类似,只是多了时差(如 2013-12-20T10:35:50.711+08:00[Asia/Shanghai])
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);
//其他的用法也是类似的 就不介绍了
ZonedDateTime z1 = ZonedDateTime.parse("2013-12-31T23:59:59Z[Europe/Paris]");
System.out.println(z1);
Duration
// 表示两个瞬时时间的时间段
Duration d1 = Duration.between(Instant.ofEpochMilli(System.currentTimeMillis() - 12323123),
// 得到相应的时差
System.out.println(d1.toDays());
System.out.println(d1.toHours());
System.out.println(d1.toMinutes());
System.out.println(d1.toMillis());
System.out.println(d1.toNanos());
// 1天时差类似的还有如ofHours()
Duration d2 = Duration.ofDays(1);
System.out.println(d2.toDays());
//提供对 java.util.Calendar 的替换,提供对年历系统的支持
Chronology c = HijrahChronology.INSTANCE;
ChronoLocalDateTime d = c.localDateTime(LocalDateTime.now());
System.out.println(d);
// 新旧日期转换
Instant instant=new Date().toInstant();
Date date=Date.from(instant);
System.out.println(instant);
System.out.println(date);
9、JSR310 规范joda-Time的区别
其实JSR310的规范领导者Stephen Colebourne,同时也是Joda-Time 的创建者,JSR310是在Joda-Time 的基础上建立的,参考了绝大部分的API,但并不是说 JSR310 = JODA-Time,下面几个比较明显的区别是:
1)包名改变,从org.joda.time 到 java.time
2)JSR310 不接受NULL值,Joda-Time 视NULL 值为0
3)JSR310 的计算机相关时间(Instant)和与人类相关的时间(DateTime)之间的差别变得更明显
4)JSR310 所有抛出的异常都是DateTimeException的子类,虽然DateTimeException 是一个RuntimeException
总结