[英]I want to convert a string to date format in java 6
My Input is like this: 我的输入是这样的:
20180718140032488266000Z-0600
and the way it should go and persist in database is like this: 它应该在数据库中保存的方式如下:
21-JUL-18 12.05.25.000000000 AM
Currently I have written code like this : 目前我已经写了这样的代码:
public static void main(String[] argv) throws ParseException {
// SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSSZ");
String dateInString = "20180718140032488266000Z-0600";
Timestamp ts = formatDate(dateInString);
System.out.println(ts);
}
private static Timestamp formatDate(String dateString) throws ParseException {
// dateString = dateString.replaceAll("Z", "0000");
SimpleDateFormat fmtTimestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS'Z'");
Date dtTimestamp1 = fmtTimestamp.parse(dateString);
Timestamp timestamp = new java.sql.Timestamp(dtTimestamp1.getTime());
return timestamp;
}
I am getting an output in this format: 2018-07-24 05:38:18.0
, my question is how can I get AM/PM also in the output. 我正在以以下格式输出:
2018-07-24 05:38:18.0
,我的问题是如何在输出中也获得AM / PM。 Please suggest solutions. 请提出解决方案。 I will be really grateful.
我将非常感激。
my question is how can I get AM/PM also in the output
我的问题是如何在输出中也获得AM / PM
Well Java 6 is very old. Java 6很老了。 But You need to check the Documentantion for this SimpleDateFromat With this you will see that the letter
a
is for am/pm Maker: 但是,您需要检查此SimpleDateFromat的Documentantion。通过此操作,您将看到字母
a
用于am / pm Maker:
a Am/pm marker Text PM
And your format is 21-JUL-18 12.05.25.000000000 AM
So in the end for adding this you need to make a new SimpleDateFormat 您的格式是
21-JUL-18 12.05.25.000000000 AM
因此最后要添加此格式,您需要制作一个新的SimpleDateFormat
public static void main(String args[]) throws IOException, ParseException {
String dateInString = "20180718140032488266000Z-0600";
Timestamp ts = formatDate(dateInString);
System.out.println(ts);
SimpleDateFormat fmtTimestamp = new SimpleDateFormat("dd-MMM-YY hh.mm.s.SSSSS a");
System.out.println(fmtTimestamp.format(ts));
}
With this you will get this... But there is better solution for this. 有了这个,您将得到……但是有更好的解决方案。 Like upgrading to a new version to Java.
就像升级到Java的新版本一样。 My humble opinion.
我的拙见。
The below code will give you date in '2018-07-31 7:39:09 PM' format.Kindly refer http://tutorials.jenkov.com/java-internationalization/simpledateformat.html#formatting-dates for additional info 下面的代码将以'2018-07-31 7:39:09 PM'的格式给您提供日期。请参考http://tutorials.jenkov.com/java-internationalization/simpledateformat.html#formatting-dates了解更多信息
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
String strDate= formatter.format(date);
System.out.println(strDate);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.