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

时间:2022-11-05 09:31:19

Is there an easy way to convert a timestamp field which is of format yyyy-MM-dd hh:mm:ss to a String of format MM-dd-yyyy hh:mm:ss. I was able to do so using substr but was wondering if there is a straighforward way of converting it. Thanks!

有没有一种简单的方法可以将格式为yyyy-MM-dd hh:mm:ss的时间戳字段转换为格式为MM-dd-yyyy hh:mm:ss的字符串。我能够使用substr这样做但是想知道是否有一种直接转换它的方式。谢谢!

4 个解决方案

#1


First of all... Date or Timestamp objects have it's own format, so you don't have to care how is stored, you need to change the format in the moment you show it, but not when you store it:

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

When you need to show/print it use SimpleDateFormat for example if you want to show Timestamp in own format (MM-dd-yyyy hh:mm:ss) you must do like this:

当你需要显示/打印它时,使用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));

#2


You can use SimpleDateFormat.

您可以使用SimpleDateFormat。

Example :

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 

#3


private static String format(Date sourceDate) throws ParseException {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    Calendar calendar = Calendar.getInstance();

    calendar.setTime(sdf.parse(sourceDate));

    return dateTimeFormat.format(calendar.getTime());
}

#4


Try like this using SimpleDateFormat:

使用SimpleDateFormat尝试这样:

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

#1


First of all... Date or Timestamp objects have it's own format, so you don't have to care how is stored, you need to change the format in the moment you show it, but not when you store it:

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

When you need to show/print it use SimpleDateFormat for example if you want to show Timestamp in own format (MM-dd-yyyy hh:mm:ss) you must do like this:

当你需要显示/打印它时,使用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));

#2


You can use SimpleDateFormat.

您可以使用SimpleDateFormat。

Example :

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 

#3


private static String format(Date sourceDate) throws ParseException {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    Calendar calendar = Calendar.getInstance();

    calendar.setTime(sdf.parse(sourceDate));

    return dateTimeFormat.format(calendar.getTime());
}

#4


Try like this using SimpleDateFormat:

使用SimpleDateFormat尝试这样:

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