我在尝试使用代码时正在解析异常
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
Run Code Online (Sandbox Code Playgroud)
例外
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
Run Code Online (Sandbox Code Playgroud)
输入:Sat Jun 01 12:53:10 IST 2013
预期产量:Jun 01,2013 12:53:10
怎么解决这个?
使用这种数字时,使用DecimalFormat不会产生任何解析异常:
123hello
这显然不是一个数字,并转换为123.0值.我该如何避免这种行为?
作为旁注,hello123确实给出了一个例外,这是正确的.
谢谢,马塞尔
这是一个例子:
public MyDate() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
sdf.setLenient(false);
String t1 = "2011/12/12aaa";
System.out.println(sdf.parse(t1));
}
Run Code Online (Sandbox Code Playgroud)
2011/12/12aaa不是有效的日期字符串.但是,该功能打印"2011年12月12日00:00:00 PST",并且不会抛出ParseException.
谁能告诉我如何让SimpleDateFormat将"2011/12/12aaa"视为无效的日期字符串并抛出异常?
我收到以下错误:'java.text.ParseException:Unparseable date:"Aug 31 09:53:19 2011"'使用以下格式: new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
有谁看到这个问题?
我正在编写一个约会程序,允许用户输入约会日期,描述和约会类型.一切正常,直到他们选择"打印范围"打印一系列日期,当他们选择这样做它告诉他们输入开始日期和结束日期时,程序然后从这些日期之间拉出所有约会并将它们显示在输出框中.
以下是我在打印范围内遇到的错误:
AppointmentNew.java:68: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date lowDate = sdf.parse(stdin.nextLine());
^
AppointmentNew.java:70: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date highDate = sdf.parse(stdin.nextLine());
^
AppointmentNew.java:77: unreported exception java.text.ParseException; must be caught or declared to be thrown
Date newCurrentDate = sdf.parse(currentDate);
Run Code Online (Sandbox Code Playgroud)
我想我应该做一个try/catch块,但我不确定如何做到这一点,并想知道是否有人可以给我一个答案或示例来解决这些错误.
以下是我的一些代码,我认为解析错误正在发生:
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AppointmentNew
{
public static void main (String[] args) throws Exception
{
if (choiceNum == 2)
{
System.out.print("\n\n\tEnter START …Run Code Online (Sandbox Code Playgroud) 使用Java Date实用程序时的真正含义是什么并且已被弃用.这是否意味着不鼓励使用,还是暗示它被禁止?
我猜测使用弃用方法是不好的做法,但我不确定并想知道.
例如,我正在尝试使用如下代码
String date = request.getParameter("date");
model.setDate(new Date(date));
Run Code Online (Sandbox Code Playgroud)
当然......这是一个高级示例,但在这种情况下,我的模型使用Date类型,我需要将请求中的日期作为String拉出并使用它创建日期.
我的工作方式很好,但它使用的是不推荐使用的方法.
编辑 - 我已经回去使用了
SimpleDateFormat formatter = new SimpleDateFormat();
model.setDate(formatter.parse(request.getParameter("date");
日期的格式为MM/DD/YYY,如07/23/2010,但我收到了ParseException
这可能是什么?
在查看了几个现有帖子之后,我仍然无法使我的SimpleDateFormat解析器工作.这是代码:
SimpleDateFormat df = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
try {
volcanoListDate = df.parse(currentValue);
} catch (ParseException e) {
Log.d("DEBUG", e.toString());
Log.d("DEBUG", currentValue);
}
Run Code Online (Sandbox Code Playgroud)
我总是以ParseException结束.以下是调试消息的输出:
06-09 23:52:17.478:DEBUG/DEBUG(2436):java.text.ParseException:Unparseable date:06-09
23:52:17.478:DEBUG/DEBUG(2436):Wed,08 Jun 2011 03:23: 55 -0500
区域设置已设置,模式看起来没问题.我哪里错了?
我试图解析日期时间字符串,SimpleDateFormat.parse()但我一直收到Unparseable日期异常.
这是我尝试解析的日期格式: 2011-10-06T12:00:00-08:00
这是我正在使用的代码:
try {
String dateStr = "2011-10-06T12:00:00-08:00";
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM d, yyyy");
Date date = dateParser.parse(dateStr);
System.out.println(dateFormatter.format(date));
} catch(Exception e) {
System.out.println(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
哪个返回此错误: java.text.ParseException: Unparseable date: "2011-10-06T12:00:00-08:00"
据我所知,这是使用SimpleDateFormat类的正确方法,但我不能流利使用Java,所以我可能会弄错.谁知道我的问题是什么?
我正在获取ParseException以下代码
String dateStr = "2011-12-22 10:56:24.389362";
String formatStr = "yyyy-MM-dd HH:mm:ss.SSSSSS";
Date testDate = null;
SimpleDateFormat sdf= new SimpleDateFormat(formatStr);
sdf.setLenient(false);
testDate = sdf.parse(dateStr);
System.out.println("CHECK DATE " + sdf.format(testDate));
Run Code Online (Sandbox Code Playgroud)
Exception in thread "main" java.text.ParseException: Unparseable date: "2011-12-22 10:56:24.389362"
at java.text.DateFormat.parse(DateFormat.java:337)
如果我注释掉该行sdf.setLenient(false),那么我会看到输出的时间差
CHECK DATE 2011-12-22 11:02:53.000362
我究竟做错了什么??
当我将特定格式应用于日期时出现解析异常。
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
try {
String s=timeSlotsArrayList.get(position).getScheduledStartTime();
Date d = df.parse(s);
times.setText(df.format(d));
}
catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
AM 正在获取而不是 PM 问题图像