简体   繁体   English

将字符串解析为Datestamp不起作用。 爪哇

[英]Parsing String to Datestamp doesn't work. Java

I'm trying to parse String in format MMM-dd-yyyy to Timestamp. 我正在尝试将MMM-dd-yyyy格式的String解析为Timestamp。 I'm doing this like below: 我这样做如下:

String dateto = "Dec-01-2013";
try{
    DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy");
    Date datetoD = (Date)datetoDF.parse(dateto);
    datetoTS = new Timestamp(datetoD.getTime());
}
catch(ParseException e)
{
    log.error("RegisteredUserDataProvider:getFilteredUsers:ParseException: " + e.getMessage());
    String stackTrace = ExceptionUtils.getStackTrace(e);
    log.error(stackTrace);
}

Why it works only with March (for example Mar-01-2013)? 为什么它仅与三月一起使用(例如2013年3月1日)? When I give any other date with other month I get 当我给其他月份指定任何其他日期时,我得到

java.text.ParseException: Unparseable date: "Dec-01-2013"

I've checked it for set of variables with every month: "Dec-01-2013", "Nov-01-2013", "Oct-01-2013", "Sep-01-2013", "Aug-01-2013", "Jul-01-2013", "Jun-01-2013", "May-01-2013", "Apr-01-2013", "Feb-01-2013", "Jan-01-2013" and "Mar-04-2013". 我每个月都检查了一组变量:“ Dec-01-2013”​​,“ Nov-01-2013”​​,“ Oct-01-2013”​​,“ Sep-01-2013”​​,“ Aug-01- 2013”​​,“ 2013年7月1日”,“ 2013年6月1日”,“ 2013年5月1日”,“ 2013年4月1日”,“ 2013年2月1日”,“ 2013年1月1日”和“ 2013年3月4日”。 It works only for Mar. Any idea why? 它仅适用于3月。知道为什么吗?

Looks like your Default Locale is different. 看起来您的默认Locale有所不同。

DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy", Locale.ENGLISH);

Your default Locale may not recognize the words "Jan", "Feb" etc. 您的默认语言环境可能无法识别单词“ Jan”,“ Feb”等。

All about Local or you are passing date Dec -01-2013 with space. 关于“本地”的所有信息,或者您正在通过空格传递日期为Dec -01-2013 Provide proper date without space such as Dec-01-2013 ,and try to print Locale.getDefault() if your default is set something like Korea then definitely you will get this exception then try to use like 提供正确的日期,没有空格,例如Dec-01-2013 ,并尝试打印Locale.getDefault()如果您的默认值设置为韩国,则肯定会得到此异常,然后尝试使用

DateFormat datetoDF = new SimpleDateFormat("MMM-dd-yyyy",Locale.ENGLISH);

You can use this. 您可以使用它。

 String dateto = "Dec-01-2013";
 Date datetoD = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dateto );
 datetoTS = new Timestamp(datetoD.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 开关盒不起作用。 爪哇 - Switch case doesn't work. Java 拆分字符串不起作用。 (未使用的局部变量的值) - Splitting String doesn't work. (value of local variable not used) Java Httpclient HttpRequestRetryHandler 不起作用。 如何实现重试? - Java Httpclient HttpRequestRetryHandler doesn't work. How to implement retrys? GlassFish不起作用。 错误:java.BindException - GlassFish doesn't work. Error: java.BindException java - FTPClient,多线程上传不起作用。 - java - FTPClient, multithread upload doesn't work. 将字符串值解析为 integer 不起作用 - Parsing string value to integer doesn't work 多线程不起作用。 什么不对? - Multithreading doesn't work. What is incorrect? 将 aws-java-sdk 添加到 pom 时,@JsonIgnore 不起作用。 为什么? - When adding aws-java-sdk to the pom, @JsonIgnore doesn't work. Why? Jsoup HTML解析可以在Java上运行,但不能在android studio上运行 - Jsoup HTML Parsing work on java but doesn't work on android studio textView 中的文本未显示,但 java 代码有效。 如果我将 autoLink="web" 标签添加到 textview,文本会显示,但 java 代码不会 - Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM