Java 尝试使用SimpleDataFormat类使用android kotlin格式化Unix日期模式(%Y-%m-%d),但没有结果

Java 尝试使用SimpleDataFormat类使用android kotlin格式化Unix日期模式(%Y-%m-%d),但没有结果,java,android,date,kotlin,simpledateformat,Java,Android,Date,Kotlin,Simpledateformat,我从一个API收到了这个日期模式,我正试图用我的android应用程序解析它,却不知道该怎么做。非常感谢您的帮助 我已经尝试了很多数据库,花了很多时间进行研究,但没有结果 And here is simple of the code i am trying : val simpleFormat = SimpleDateFormat("%Y-%m-%d", Locale.getDefault()) simpleFormat.timeZone = TimeZone.getDefault() val

我从一个API收到了这个日期模式,我正试图用我的android应用程序解析它,却不知道该怎么做。非常感谢您的帮助

我已经尝试了很多数据库,花了很多时间进行研究,但没有结果

And here is simple of the code i am trying :

val simpleFormat = SimpleDateFormat("%Y-%m-%d", Locale.getDefault())
simpleFormat.timeZone = TimeZone.getDefault()
val now: Calendar = Calendar.getInstance()!!

Log.d("datedate", simpleFormat.format(now.time).toString())
我收到以下结果%2019-%20-%25


我正在寻找一个正常的日期,例如
2019-4-25

从日期格式化程序代码中删除
%
符号或将其更改为下面的代码。它应该正确地显示日期


val simpleFormat=SimpleDateFormat(“yyyy-MM-dd”,Locale.getDefault())
从日期格式化程序代码中删除
%
符号或将其更改为以下代码。它应该正确地显示日期


val simpleFormat=SimpleDateFormat(“yyyy-MM-dd”,Locale.getDefault())

在SimpleDateFormat中,指定日期的输出方式

在您的示例中,您有
%Y-%M-%d
,因此您将得到
%2019-%01-%01

如果您更改为
Y-M-d
您将获得
2019-01-01

在代码中

val simpleFormat = SimpleDateFormat("Y-m-d", Locale.getDefault())

您可以在指定日期输出方式的SimpleDateFormat中查看更多信息

在您的示例中,您有
%Y-%M-%d
,因此您将得到
%2019-%01-%01

如果您更改为
Y-M-d
您将获得
2019-01-01

在代码中

val simpleFormat = SimpleDateFormat("Y-m-d", Locale.getDefault())

您可以查看Unix日期格式字符串的更多信息,例如您的
%Y-%m-%d
,它遵循的系统与Java的
日期时间格式设置程序所使用的格式模式字符串或麻烦且过时的
SimpleDateFormat
完全不同,例如
yyyy-MM-dd
(另一方面,这两个Java类使用非常相似但不完全相同的格式模式字符串)

我认为您需要决定的第一件事是要涵盖所有可能的Unix格式字符串还是最常见的字符串。Unix格式字符串语言非常复杂。很容易将
%Y
转换为
yyyy
%m
转换为
MM
,等等,
%%
转换为一个
%
。您需要在单引号中引用任何前面没有百分比符号的字母,以告诉Java格式化程序它们不是模式字母。例如,Unix
%d of%B
在Java中应该是
dd'of'MMMM
。Unix还包括century的
%C
,Java不支持(或者只通过
DateTimeFormatterBuilder
和自制的
TemporalField
,您就不想麻烦了).POSIX有大约三个字符扩展名,
%Ec
%Ec
%Ex
,您可以在百分号和字母之间插入
-
0
,以控制填充。最后,GNU和BSD之间存在差异,因此您需要决定使用哪一个

对于从Unix到Java的转换,使用正则表达式可能会有很长的路要走,但是如果您确实想涵盖所有情况,那么从长远来看,手工编写解析器将更容易

黑客:String.format
String.Java中的format
使用的格式字符串与Unix中的
date
使用的格式字符串有点类似。主要区别在于Java在百分号和字母之间有一个
t
%Y
在Unix中是
%tY
在Java中,等等。因此插入一个:

    String unixFormatString = "%Y-%m-%d";
    // Insert 't'
    String javaFormatString = unixFormatString.replaceAll("(%)(\\w)", "$1t$2");

    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Antarctica/Rothera"));

    // We need to pass the datetime to String.format as many times as there are
    // format specifiers in the format string. There cannot be more than half of the
    // length of the string. Passing too many doesn’t harm, so just pass this many.
    Object[] formatArgs = Collections.nCopies(javaFormatString.length() / 2, now).toArray();
    String formattedDateTime = String.format(javaFormatString, formatArgs);
    System.out.println(formattedDateTime);
当我今天运行这个片段时,我得到:

2019-01-26

它没有涵盖很多情况,但涵盖了许多最常见的情况

链接

Unix日期格式字符串,例如您的
%Y-%m-%d
,遵循的系统与Java的
DateTimeFormatter
或麻烦且过时的
SimpleDateFormat
所使用的格式模式字符串完全不同,例如
yyyy-MM-dd
(另一方面,这两个Java类使用非常相似但不完全相同的格式模式字符串)

我认为您需要决定的第一件事是要涵盖所有可能的Unix格式字符串还是最常见的字符串。Unix格式字符串语言非常复杂。很容易将
%Y
转换为
yyyy
%m
转换为
MM
,等等,
%%
转换为一个
%
。您需要在单引号中引用任何前面没有百分比符号的字母,以告诉Java格式化程序它们不是模式字母。例如,Unix
%d of%B
在Java中应该是
dd'of'MMMM
。Unix还包括century的
%C
,Java不支持(或者只通过
DateTimeFormatterBuilder
和自制的
TemporalField
,您就不想麻烦了).POSIX有大约三个字符扩展名,
%Ec
%Ec
%Ex
,您可以在百分号和字母之间插入
-
0
,以控制填充。最后,GNU和BSD之间存在差异,因此您需要决定使用哪一个

对于从Unix到Java的转换,使用正则表达式可能会有很长的路要走,但是如果您确实想涵盖所有情况,那么从长远来看,手工编写解析器将更容易

黑客:String.format
String.Java中的format
使用的格式字符串与Unix中的
date
使用的格式字符串有点类似。主要区别在于Java在百分号和字母之间有一个
t
%Y
在Unix中是
%tY
在Java中,等等。因此插入一个:

    String unixFormatString = "%Y-%m-%d";
    // Insert 't'
    String javaFormatString = unixFormatString.replaceAll("(%)(\\w)", "$1t$2");

    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Antarctica/Rothera"));

    // We need to pass the datetime to String.format as many times as there are
    // format specifiers in the format string. There cannot be more than half of the
    // length of the string. Passing too many doesn’t harm, so just pass this many.
    Object[] formatArgs = Collections.nCopies(javaFormatString.length() / 2, now).toArray();
    String formattedDateTime = String.format(javaFormatString, formatArgs);
    System.out.println(formattedDateTime);
当我今天运行这个片段时,我得到:

2019-01-26

它没有涵盖很多情况,但涵盖了许多最常见的情况