将java.sql.timestamp从yyyy-MM-dd hh:MM:ss转换为MM-dd yyyy hh:MM:ss

将java.sql.timestamp从yyyy-MM-dd hh:MM:ss转换为MM-dd yyyy hh:MM:ss,java,Java,是否有一种简单的方法可以将格式为yyyy-MM-dd hh:MM:ss的时间戳字段转换为格式为MM-dd-yyyy hh:MM:ss的字符串。 我可以使用substr来实现这一点,但我想知道是否有一种严格的方式来转换它。 谢谢 使用像这样尝试: DateFormat d = new SimpleDateFormat("MM-dd-yyyy HH:mm"); String str= d.format(timestamp); System.out.println(str); 使用这样尝试: Dat

是否有一种简单的方法可以将格式为
yyyy-MM-dd hh:MM:ss
的时间戳字段转换为格式为
MM-dd-yyyy hh:MM:ss
的字符串。 我可以使用substr来实现这一点,但我想知道是否有一种严格的方式来转换它。
谢谢

使用像这样尝试:

DateFormat d = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String str= d.format(timestamp);
System.out.println(str);

使用这样尝试:

DateFormat d = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String str= d.format(timestamp);
System.out.println(str);

您可以使用
SimpleDataFormat

例如:

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = input.parse("your date string goes here");

SimpleDateFormat output = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
System.out.println(output.format(d));  // will return date as string in format MM-dd-yyyy hh:mm:ss 

您可以使用
SimpleDataFormat

例如:

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = input.parse("your date string goes here");

SimpleDateFormat output = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
System.out.println(output.format(d));  // will return date as string in format MM-dd-yyyy hh:mm:ss 

首先<代码>日期或
时间戳
对象有自己的格式,因此您不必关心如何存储,您需要在显示时更改格式,而不是在存储时更改格式

DateFormat d = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String str= d.format(timestamp);
System.out.println(str);
当您需要显示/打印它时,请使用
SimpleDateFormat
,例如,如果您想以自己的格式(
MM dd yyyy hh:MM:ss
)显示
时间戳
),您必须执行以下操作:

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
System.out.println("My date formatted is: " + sdf.format(timestamp));

首先<代码>日期或
时间戳
对象有自己的格式,因此您不必关心如何存储,您需要在显示时更改格式,而不是在存储时更改格式

DateFormat d = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String str= d.format(timestamp);
System.out.println(str);
当您需要显示/打印它时,请使用
SimpleDateFormat
,例如,如果您想以自己的格式(
MM dd yyyy hh:MM:ss
)显示
时间戳
),您必须执行以下操作:

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
System.out.println("My date formatted is: " + sdf.format(timestamp));

您认为使用
SimpleDataFormat
不是一种简单的方法吗?您对“时间戳字段”的确切含义是什么?数据库表中的列?Swing应用程序中的输入字段(例如
JTextField
)?HTML页面中的表单元素?记住,日期或时间戳的可能副本没有格式。因此,您的问题应该是:“如何以
MM dd yyyy hh:MM:ss
格式获取
java.sql.Timestamp
”您认为使用
SimpleDataFormat
不是一种直接的方法吗?您对“时间戳字段”的确切含义是什么?数据库表中的列?Swing应用程序中的输入字段(例如
JTextField
)?HTML页面中的表单元素?记住,日期或时间戳的可能副本没有格式。因此,您的问题应该是:“我认为如何在
MM dd yyyy hh:MM:ss
格式中获取
java.sql.Timestamp
”。。。最好的答案,当然。在我看来。。。绝对是最好的答案。