如何从 android 中的 localdatetime 中删除 T 和 Zone

How to remove T and Zone from localdatetime in android

你好,下面的代码想要从 LocalDateTime 中删除 T 和 Zone。

谁能帮我解决这个问题。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    LocalDateTime time = LocalDateTime.now();
                        firstDayOfQuarteropp = time.with(time.getMonth().firstMonthOfQuarter())
                                .with(TemporalAdjusters.firstDayOfMonth());
                        Log.d("firstDayOfQuarteropp", String.valueOf(firstDayOfQuarteropp));
                        lastDayOfQuarteropp = firstDayOfQuarteropp.plusMonths(2)
                                .with(TemporalAdjusters.lastDayOfMonth());
                        Log.d("lastDayOfQuarteropp", String.valueOf(lastDayOfQuarteropp));
                }

实际输出:

2020-04-01T11:54:05.514

预期输出:

2020-04-01 11:54:05

您需要将 DateTimeFormatter 与 LocalDateTime 一起使用

    LocalDateTime time = LocalDateTime.now(); 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String formatDateTime = time.format(formatter); 
    Log.d("FormattedTime", "Time: " + formatDateTime);

您必须格式化本地日期和时间。

public String formatedDate() {
    LocalDateTime time = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return time.format(formatter);
}

并将其用作:

String dt=formatedDate();