Tag: string parsing

从绝对路径中提取相对路径

这是一个看似简单的问题,但我无法以干净的方式进行。 我有一个文件路径如下: /这/是/的/绝对/路径/到/的/位置/的/我的/文件 我需要的是从上面给出的路径中提取/ of / my / file,因为那是我的相对路径。 我想这样做的方式如下: String absolutePath = “/this/is/an/absolute/path/to/the/location/of/my/file”; String[] tokenizedPaths = absolutePath.split(“/”); int strLength = tokenizedPaths.length; String myRelativePathStructure = (new StringBuffer()).append(tokenizedPaths[strLength-3]).append(“/”).append(tokenizedPaths[strLength-2]).append(“/”).append(tokenizedPaths[strLength-1]).toString(); 这可能会满足我的直接需求,但有人可以提出一种更好的方法从java中提供的路径中提取子路径吗? 谢谢

使用Java 8 java.time api解析ISO时间戳(仅限标准版)

我在例子中从字符串中获取毫秒数时遇到麻烦。 到目前为止,我已尝试过这三种不同的方式,示例显示了最新的尝试。 似乎总是可以归结为TemporalAccessor不支持ChronoField 。 如果我能成功构建一个Instant实例,我可以使用toEpochMilli() 。 String dateStr = “2014-08-16T05:03:45-05:00” TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr); Instant creationDate = Instant.from(creationAccessor); 请提供简明的答案(不要从头开始构建格式化程序)并仅使用java 8标准发行版(我可以使用Joda,但希望避免依赖项)。 编辑:上面代码中的java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=-18000},ISO resolved to 2014-08-16T05:03:45 of type java.time.format.Parsed抛出: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=-18000},ISO resolved to 2014-08-16T05:03:45 of type java.time.format.Parsed