PHP8 —— New String Helpers

新增了三个字符串函数,str_starts_with, str_ends_with, str_contains, PHP 的函数这么方便,很难想象竟然一直没有这几个。 str_starts_with 判断字符串是否以另一个字符串开头,在PHP7以及之前 $id = 'inv_abcdefgh'; $result = strpos($id, 'inv_') === 0; var_dump($result); // true PHP8 中可以直接这么写 $result = str_starts_with($id, 'inv_'); str_ends_with 判断字符串是否以另外一个字符串结尾,在 PHP7 及之前,比较麻烦,通常是这么写 $id = 'abcd_inv'; $result = strpos(strrev($id), strrev('_inv')) === 0; 或者 $result = substr($id, -1 * strlen('_inv')) === '_inv'; 或者上正则吧 $result = preg_match('/_inv$/', $id) === 1; 看起来都是比较麻烦的。PHP8 里面可以简化成下面这样了 $id = 'abcd_inv'; $result = str_ends_with($id, '_ind'); str_contains 字符串包含,PHP8 之前一般就是 strpos 来实现了 $url = 'https://example?for=bar'; $result = strpos($url, '?') !== FALSE; PHP8 就直接一点 $result = str_contains($url, '?');

Java学习笔记 —— 使用 Date-Time 基本类

Date-Time中的类都产生不可变的实例,因此它们是线程安全的。 它们也没有公共构造函数,因此每个都使用工厂方法实例化。
需要特别注意两种方法:now和of。

now

now方法用于基于当前日期或时间创建实例。

System.out.println("LocalDate.now(): " + LocalDate.now());
System.out.println("LocalTime.now(): " + LocalTime.now());
System.out.println("LocalDateTime.now(): " + LocalDateTime.now()); 
System.out.println("ZonedDateTime.now():" + ZonedDateTime.now());
System.out.println("Instant.now(): " + Instant.now());

结果如下:

LocalDate.now(): 2020-03-10
LocalTime.now(): 14:23:04.823013
LocalDateTime.now(): 2020-03-10T14:23:04.824918
ZonedDateTime.now():2020-03-10T14:23:04.826819+08:00[Asia/Shanghai]
Instant.now(): 2020-03-10T06:23:04.833487Z

所有输出值均使用ISO 8601标准格式。

  • 对于日期,基本格式为yyyy-MM-dd。
  • 对于时间,格式为hh:mm:ss.sss。
  • LocalDateTime 则使用大写字母T连接日期和时间。
  • 带时区的日期时间,带上了以UTC为基础的数字偏移量(+08:00),并附加地区名称(Asia/Shanghai)。
  • Instant的toString方法输出为UTC时间(Zulu),精确到纳秒。
    Year,YearMonth和ZoneId类中也有now方法。

of

of 方法用于产生新值。
对于LocalDate,参数为项是年,月(枚举或整数)和月中的某天。
对于LocalTime,有多个重载,用于小时,分钟,秒和纳秒组合使用。
LocalDateTime的of方法结合前两个类。

System.out.println("First landing on the Moon:");
LocalDate moonLandingDate = LocalDate.of(1969, Month.JULY, 20);
LocalTime moonLandingTime = LocalTime.of(20, 18);
System.out.println("Date: " + moonLandingDate);
System.out.println("Time: " + moonLandingTime);
System.out.println("Neil Armstrong steps onto the surface: ");
LocalTime walkTime = LocalTime.of(20, 2, 56, 150_000_000);
LocalDateTime walk = LocalDateTime.of(moonLandingDate, walkTime);
System.out.println(walk)

输出如下:

Date: 1969-07-20
Time: 20:18
Neil Armstrong steps onto the surface: 
1969-07-20T20:02:56.150

时区Id

ZoneId 有两种类型的时区ID:

  • 相对于UTC/Greenwich标准的固定偏移量,例如-05:00
  • 地理区域,例如 America/Chicago

从技术上讲,存在第三种ID类型,它是从祖鲁时间开始的偏移量,用数字和Z表示
可以使用ZoneId 的 systemDefault 静态方法获取ZoneId的当前值,getAvailableZoneIds静态方法可以获取到完整的区域Id列表。
ZoneRulesProvider加载可以ZoneRules规则类,ZoneRules类具有诸如isDaylightSavings之类的方法。

Set<String> regionNames = ZoneId.getAvailableZoneIds(); 
System.out.println("There are " + regionNames.size() + " region names");

对于jdk1.8.0_131,有600个区域名称。

其他方法

DataTime Api 使用标准前缀作为方法名称。

Method Type Use
of Static factory Creates an instance
from Static factory Converts input parameters to target class
parse Static factory Parses an input string
format Instance Produces formatted output
get Instance Returns part of an object
is Instance Queries the state of the object
with Instance Creates a new object by changing one element of an existing one
plus, minus Instance Creates a new object by adding or subtracting from an existing one
to Instance Converts an object to another type
at Instance Combines this object with another

at方法的作用是为时间日期制定一个时区。

    LocalDateTime dateTime = LocalDateTime.of(2017, Month.JULY, 4, 13, 20, 10);
    ZonedDateTime nyc = dateTime.atZone(ZoneId.of("America/New_York"));
    System.out.println(nyc);
    ZonedDateTime london = nyc.withZoneSameInstant(ZoneId.of("Europe/London"));
    System.out.println(london);

结果为:

2017-07-04T13:20:10-04:00[America/New_York]
2017-07-04T18:20:10+01:00[Europe/London]

withZoneSameInstant 将 ZonedDateTime 转换成另一个时区来表示。
程序包中有两个枚举:Month和DayOfWeek。 在标准日历(从1月到12月)中,每个月对应一个常数。

System.out.println("Days in Feb in a leap year: " + Month.FEBRUARY.length(true));
System.out.println("Day of year for first day of Aug (leap year): " + Month.AUGUST.firstDayOfYear(true));
System.out.println("Month.of(1): " + Month.of(1));
System.out.println("Adding two months: " + Month.JANUARY.plus(2));
System.out.println("Subtracting a month: " + Month.MARCH.minus(1));

输出为:

Days in Feb in a leap year: 29
Day of year for first day of Aug (leap year): 214
Month.of(1): JANUARY
Adding two months: MARCH
Subtracting a month: FEBRUARY

DayOfWeek枚举的常量表示从星期一到星期日的七个工作日,int值都遵循ISO标准,MONDAY为1,SUNDAY为7。

评论

此博客中的热门博文

D3js Data-binding basics

JavaScript 数据类型

Vue3新特性(1) —— Vite