【问题标题】:How to get a specific part of a string in java如何在java中获取字符串的特定部分
【发布时间】:2020-06-13 14:53:34
【问题描述】:

我正在开发一个从互联网获取日期的 Android 应用。我所拥有的是一个完整的日期格式,例如:2020-06-13T16:21:15.239920+02:00。我想得到一个月中的哪一天(在这种情况下是 13)。

【问题讨论】:

  • 您可以使用字符串函数根据字符位置获取日期,也可以使用 java 中提供的数据/时间格式化程序来获取日期
  • @NitinBisht 但如何根据角色位置获得它(我是初学者)
  • 如果你只想要这种格式,你可以使用 YOUR_STRING.substring(8,10);
  • @MohamedHassan 检查substring 方法
  • @Everyone 请不要使用字符串函数。使用投票最多的答案所说的日期时间函数。

标签: java android string datetime dayofmonth


【解决方案1】:

如果您至少使用 API 级别 26,那么您可以使用 ZonedDateTime 类,因为您的字符串使用该类可以理解的默认格式。

ZonedDateTime zdt = ZonedDateTime.parse("2020-06-13T16:21:15.239920+02:00");
int d = zdt.getDayOfMonth();

或者,如果格式不变,只需使用方法substring()

String dd = "2020-06-13T16:21:15.239920+02:00".substring(8, 10);

如果格式不固定,我建议regular expression 或将ZonedDateTimeDateTimeFormatter 结合使用

【讨论】:

  • ZonedDateTime 不是必需的。 OffsetDateTime 会这样做,因为字符串包含偏移量 +02:00,而不是像亚洲/耶路撒冷这样的时区。否则一个很好的答案。
【解决方案2】:

您的日期/时间字符串符合DateTimeFormatter.ISO_OFFSET_DATE_TIME。您可以使用java.time API 获取月份中的日期,如下所示:

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        String dateTimeStr = "2020-06-13T16:21:15.239920+02:00";
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr);
        int day = odt.getDayOfMonth();
        System.out.println(day);
    }
}

输出:

13

如果您不能使用Java SE 8 Date and Time,请检查ThreeTenABPHow to use ThreeTenABP in Android Project

【讨论】:

  • OffsetDateTime.parse 默认解释 DateTimeFormatter.ISO_OFFSET_DATE_TIME 中的字符串。因此,您可以通过删除指定格式和 ZoneOffset 来简化 OP 的情况,并获得相同的结果。
  • @jrahhali - 感谢您的建议。我已经更新了我的答案以包含它。
【解决方案3】:

拆分字符串并提取数据总是一个坏主意,因为一旦日期、月份或日期字符串的任何部分发生更改,字符串将更改为其他值,并且字符串索引也会更改

所以使用DateTimeFormatter

import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class SDF {

    public static void main(String[] args) {
        final TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSxxx")
                .parse("2020-06-13T16:21:15.239920+02:00");

        int dayOfMonth = MonthDay.from(parse).getDayOfMonth();
        System.out.println(dayOfMonth);
    }
}

【讨论】:

    【解决方案4】:

    这个变量应该有一个方法getDayOfMonth()可用

    private int parseDate(ZonedDateTime date) {
        int day = date.getDayOfMonth();
        return day;
    }
    

    【讨论】:

    • 给定的输入2020-06-13T16:21:15.239920+02:00 适合OffsetDateTime 类而不是ZonedDateTime。请参阅Answer by Avinash
    【解决方案5】:

    您可以使用字符串拆分。只需将整个日期格式放入 String 中,然后创建一个 new String Array String[] array= str.split("[-T]");,然后您就可以从 array[2] 获取结果。

    String str = "2020-06-13T16:21:15.239920+02:00";
    String[] array= str.split("[-T]");
    
    System.out.println("OUTPUT: " + array[2]);
    
    

    输出: 13

    【讨论】:

    【解决方案6】:
    String str = "2020-06-13T16:21:15.239920+02:00";
    String substr = "";
    
    // prints the substring after index 7(8 includes) till index 9(10 excludes)
    substr = str.substring(8, 10);
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2021-08-12
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多