如何在Java中重新格式化字符串日期
不久前,我在VB中解决了这个问题,并认为可以轻松地将其转换为Java。输入以字符串形式输入,格式如下: “年月日” 我想将其更改为以下格式: “年月日” 其中仅显示最后两年的数字。不久前我写了这个VB,它就是这样做的:如何在Java中重新格式化字符串日期,java,Java,不久前,我在VB中解决了这个问题,并认为可以轻松地将其转换为Java。输入以字符串形式输入,格式如下: “年月日” 我想将其更改为以下格式: “年月日” 其中仅显示最后两年的数字。不久前我写了这个VB,它就是这样做的: Function DateFormat(ByVal myDate As String) As String Dim reformat As Date reformat = Date.Parse(myDate, Nothing) Return Format(
Function DateFormat(ByVal myDate As String) As String
Dim reformat As Date
reformat = Date.Parse(myDate, Nothing)
Return Format(reformat, "MM/dd/yy").ToString()
End Function
我如何在Java中执行完全相同的操作,以便正确地重新格式化日期并将其作为原始字符串返回?我有类似的东西,但它不能正常工作:
public static String DateFormat(String myDate){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try{
Date formattedDate = formatter.parse(myDate);
return formattedDate.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
我不知道如何使它成为我需要的格式,因为我找不到任何类似于VB的format()函数的东西。提前谢谢。如果我理解你的问题,你可以用一双 输出为
07/15/14
如果我理解你的问题,你可以用一双 输出为
07/15/14
SimpleDataFormat采用多种不同的格式。我相信你想要的格式已经内置,可以像这样访问
Date date = new Date();
Format formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
System.out.println(s);
SimpleDataFormat采用多种不同的格式。我相信你想要的格式已经内置,可以像这样访问
Date date = new Date();
Format formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
System.out.println(s);
你基本上已经差不多明白了,只需要应用新的格式
public static String DateFormat(String myDate){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = formatter.parse(myDate);
formatter.applyPattern("MM/dd/yy");
return formatter.format(date);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
你基本上已经差不多明白了,只需要应用新的格式
public static String DateFormat(String myDate){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = formatter.parse(myDate);
formatter.applyPattern("MM/dd/yy");
return formatter.format(date);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
试试这个:
public static String DateFormat(String myDate) throws ParseException {
SimpleDateFormat inFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yy");
Date parsedInDate = inFormat.parse(myDate);
return outFormat.format(parsedInDate);
}
开始时,我们声明两个日期格式化程序,然后从输入字符串创建日期对象,最后以新格式生成字符串。尝试以下操作:
public static String DateFormat(String myDate){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = formatter.parse(myDate);
formatter.applyPattern("MM/dd/yy");
return formatter.format(date);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static String DateFormat(String myDate) throws ParseException {
SimpleDateFormat inFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yy");
Date parsedInDate = inFormat.parse(myDate);
return outFormat.format(parsedInDate);
}
首先,我们声明两个日期格式化程序,然后从输入字符串创建日期对象,最后我们以新格式生成字符串。希望能有所帮助:-)我根本不喜欢Java,但在.NET中,您可以将“日期格式”参数传递给
toString()
日期类型的方法,以便您精确地确定输出格式。可能的重复和可能的重复可能会有所帮助:-)我一点也不喜欢Java,但在.NET中,您可以将“日期格式”参数传递给toString()
日期类型的方法,以便您精确地确定输出格式。此类型的可能重复和可能重复正是我所需要的。我分析错了。非常感谢你的帮助。这正是我所需要的。我分析错了。非常感谢您的帮助。这完全满足了我对函数的要求,无需使用全局变量。谢谢,这基本上是我的答案。这里,您正在为常量DateFormat的每个方法调用实例化临时对象。这完全满足了我对函数的要求,而不使用全局变量。谢谢,这基本上是我的答案。在这里,您正在为常量DateFormat的每个方法调用实例化临时对象。
public static String DateFormat(String myDate){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = formatter.parse(myDate);
formatter.applyPattern("MM/dd/yy");
return formatter.format(date);
}catch(Exception e){
e.printStackTrace();
return null;
}
}