Android 如何将数据从一种格式转换为另一种格式

Android 如何将数据从一种格式转换为另一种格式,android,string,dataformat,Android,String,Dataformat,我的日期采用这种格式yyyy-mm-dd,我将日期保存为字符串格式。现在我有了字符串,我想知道如何在3个不同的字符串中获得yyyy、mm或dd 如果您确定每次都是相同的格式,您可以对该字符串进行字符串拆分,并从结果数组中获取值 String date = "2011-06-15"; String[] dates = date.split("-"); String year = dates[0]; String month = dates[1]; String day = dates[2];

我的日期采用这种格式
yyyy-mm-dd
,我将日期保存为字符串格式。现在我有了字符串,我想知道如何在3个不同的字符串中获得
yyyy
mm
dd

如果您确定每次都是相同的格式,您可以对该字符串进行字符串拆分,并从结果数组中获取值

String date = "2011-06-15";

String[] dates = date.split("-");

String year = dates[0];
String month = dates[1];
String day = dates[2];

如果您的日期以不同的格式结束,这将不起作用。

如果您确定每次都是以相同的格式结束,则可以对该字符串进行字符串拆分,并从结果数组中获取值

String date = "2011-06-15";

String[] dates = date.split("-");

String year = dates[0];
String month = dates[1];
String day = dates[2];
data = "2011-06-15";
String[] strings = data.split("-");
// strings[0] = "2011"
// strings[1] = "06"
// strings[2] = "15"

如果您的日期以不同的格式结束,这将不起作用。

您在这里尝试了DateFormat吗?我没有DateFormat,我有一个字符串:String data=“2011-06-15”类似的字符串…我需要3个不同的字符串中的2011,06和15。您在这里尝试了DateFormat吗?我没有DateFormat,我有一个字符串:String data=“2011-06-15”类似的东西…我需要3个不同的字符串中的2011、06和15
data = "2011-06-15";
String[] strings = data.split("-");
// strings[0] = "2011"
// strings[1] = "06"
// strings[2] = "15"