java 如何将给定的字符串格式解析为 dd-MMM-yyyy hh:mm:ss:aa?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37183113/
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
How to parse given string format to dd-MMM-yyyy hh:mm:ss:aa?
提问by Star
I can get the output as Wed May 11 15:36:08 IST 2016
, but how do I convert the date to a string with the required format?
我可以获得输出为Wed May 11 15:36:08 IST 2016
,但是如何将日期转换为具有所需格式的字符串?
Required format is: 12-05-2016 16:05:08 pm
所需格式为: 12-05-2016 16:05:08 pm
What I tried is,
我试过的是,
public class Test {
public static void main(String args[]) throws ParseException{
String epoche="1462961108000";
Long initialLogTime = Long.valueOf(epoche);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(initialLogTime);
Calendar fromDateTime = calendar;
Calendar toDateTime = fromDateTime;
toDateTime.add(Calendar.MINUTE, 30);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss:aa");
String datestring = String.valueOf(fromDateTime.getTime());
String datestring1 = String.valueOf(toDateTime.getTime());
System.out.println(datestring); //here output is Wed May 11 15:36:08 IST 2016
System.out.println(datestring1); // here output is Wed May 11 15:36:08 IST 2016
Date dates = dateFormat.parse(datestring);
Date date1s = dateFormat.parse(datestring1);
System.out.println(dates);
System.out.println(date1s);
}
}
The error I am getting is:
我得到的错误是:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed May 11 16:05:08 IST 2016"
at java.text.DateFormat.parse(DateFormat.java:357)
at test.Test.main(Test.java:27)
回答by Sanjeev
You need to format your dates accordingly. This shall help you
您需要相应地格式化您的日期。这将帮助你
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
System.out.println(dateFormat.format(fromDateTime.getTime()));
System.out.println(dateFormat.format(toDateTime.getTime()));
回答by sotondolphin
if you are using java 8, you can use
如果您使用的是 java 8,则可以使用
LocalDate date = LocalDate.now();
LocalDate 日期 = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss:aa");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss:aa");
System.out.println(date.format(formatter));
System.out.println(date.format(formatter));
回答by Ankur Singhal
Please try this
请试试这个
public static void main(String[] args) {
String epoche = "1462961108000";
Date date = new Date(Long.parseLong(epoche));
DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:aa");
String strDate = sdf.format(date);
System.out.println(strDate);
}
Output
输出
11-05-2016 15:35:08:PM
回答by Digvijay Machale
In Android, Pass String Like 11/10/2017 11:16:46to function ConvertDateTime
在 Android 中,将String Like 11/10/2017 11:16:46 传递给函数ConvertDateTime
public String ConvertUpdate(String strDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date d = simpleDateFormat.parse(strDate);
simpleDateFormat = new SimpleDateFormat("dd MMM yy hh:mm a");
return simpleDateFormat.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
output 10 Nov 17 11:16 AM
输出 10 11 月 17 日上午 11:16