如何移动字符串中的字符位置以实现日期格式?
How to move characters position in a String to achieve a date format?
在 Java (Android Studio) 中,我有一个从数据库 (Firebase) 中获取的字符串 20190329
,这是一个日期,但我想打印它以这种格式输出:29-03-2019
。所以,我只需要将 2019
移动到字符串的末尾,将 29
移动到字符串的开头。我知道这可以通过使用循环来完成,但是有没有更简单的方法来实现它?
我已经有了添加 -
并且运行良好的代码:
StringBuilder sb = new StringBuilder(date); //the variable 'date' contains 20190329 or any other date from the database.
sb.insert(2, "-");
sb.insert(5, "-");
sb.insert(8, "-");
您可以而且应该使用 java.time
来解析和格式化日期对象:
public static void main(String[] args) {
// use a LocalDate and a DateTimeFormatter with a fitting format to parse the String
LocalDate date = LocalDate.parse("20190329", DateTimeFormatter.ofPattern("yyyyMMdd"));
// print it (or return it as String) using a different format
System.out.println(date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}
这段代码的输出是
29-03-2019
大多数 java.time
功能已向后移植到 ThreeTen-Backport 项目中的 Java 6 和 Java 7。
进一步适用于 Android(ThreeTenABP 中。
见。
使用str.substring()
尝试这样做。子字符串方法 returns 从开始索引到结束索引的子字符串。
String day = data.substring(6,8);
String month = data.substring(4,6);
String year = data.substring(0,4);
String newDate = day + month + year;
StringBuilder sb = new StringBuilder(date); //the variable 'date' contains 20190329 or any other date from the database.
sb.insert(2, "-");
sb.insert(5, "-");
sb.insert(8, "-");
您可以通过将字符串转换为日期然后再转换回字符串(以您想要的格式)来实现。
这两个动作都可以通过使用 SimpleDateFormat
class.
来实现
例如(在测试 class 中包装代码当然是为了测试它),
@Test
public void test() throws ParseException {
String date = "20190329";
Date date_ = new SimpleDateFormat("yyyyMMdd").parse(date);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Formatted Date : " + sdf.format(date_));
}
最终会打印 Formatted Date : 29-03-2019
在 Java (Android Studio) 中,我有一个从数据库 (Firebase) 中获取的字符串 20190329
,这是一个日期,但我想打印它以这种格式输出:29-03-2019
。所以,我只需要将 2019
移动到字符串的末尾,将 29
移动到字符串的开头。我知道这可以通过使用循环来完成,但是有没有更简单的方法来实现它?
我已经有了添加 -
并且运行良好的代码:
StringBuilder sb = new StringBuilder(date); //the variable 'date' contains 20190329 or any other date from the database.
sb.insert(2, "-");
sb.insert(5, "-");
sb.insert(8, "-");
您可以而且应该使用 java.time
来解析和格式化日期对象:
public static void main(String[] args) {
// use a LocalDate and a DateTimeFormatter with a fitting format to parse the String
LocalDate date = LocalDate.parse("20190329", DateTimeFormatter.ofPattern("yyyyMMdd"));
// print it (or return it as String) using a different format
System.out.println(date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}
这段代码的输出是
29-03-2019
大多数 java.time
功能已向后移植到 ThreeTen-Backport 项目中的 Java 6 和 Java 7。
进一步适用于 Android(
见
使用str.substring()
尝试这样做。子字符串方法 returns 从开始索引到结束索引的子字符串。
String day = data.substring(6,8);
String month = data.substring(4,6);
String year = data.substring(0,4);
String newDate = day + month + year;
StringBuilder sb = new StringBuilder(date); //the variable 'date' contains 20190329 or any other date from the database.
sb.insert(2, "-");
sb.insert(5, "-");
sb.insert(8, "-");
您可以通过将字符串转换为日期然后再转换回字符串(以您想要的格式)来实现。
这两个动作都可以通过使用 SimpleDateFormat
class.
例如(在测试 class 中包装代码当然是为了测试它),
@Test
public void test() throws ParseException {
String date = "20190329";
Date date_ = new SimpleDateFormat("yyyyMMdd").parse(date);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Formatted Date : " + sdf.format(date_));
}
最终会打印 Formatted Date : 29-03-2019