java 将当前时间戳转换为 DD MMM YYYY 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26233529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Current Timestamp to DD MMM YYYY format
提问by user10012014
I am trying to convert current timestamp to DD MMM YYYY format.
我正在尝试将当前时间戳转换为 DD MMM YYYY 格式。
but i don't know why it is giving me an error unparsable.
但我不知道为什么它给我一个无法解析的错误。
code:
代码:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
System.out.println(format.format(fechaNueva));
working but I want to do
工作,但我想做
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
gives me error of unparsable.
给了我无法解析的错误。
java.text.ParseException: Unparseable date: "2014-10-07 15:38:29.876"
java.text.ParseException:无法解析的日期:“2014-10-07 15:38:29.876”
回答by agad
You can't use ts.toString()
, you always need to use the same formatter.
Use just:
你不能使用ts.toString()
,你总是需要使用相同的格式化程序。仅使用:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(ts));
回答by PatSham
Following worked for me
以下对我来说有效
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));
Output:
输出:
07 Oct 2014, 15:46
You were trying to parse date in a format you wanted it to be formatted instead of format in which it is already present.
您试图以您希望对其进行格式化的格式而不是它已经存在的格式来解析日期。
回答by Krishna
Simply u can do this
你可以这样做
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println("Formatted "+dateFormat.format(timestamp));
hope this helps.
希望这可以帮助。
OutPut:-
输出:-
Formatted 07 Oct 2014, 15:59