我真的在这个问题上摸不着头脑.我一直在使用SimpleDateFormat没有麻烦的s,但是现在,使用SimpleDateFormat来解析日期(仅有时)只是完全错误.
特别:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse("2009-08-19 12:00:00");
System.out.print(date.toString());
Run Code Online (Sandbox Code Playgroud)
打印字符串Wed Aug 19 00:00:00 EDT 2009.有没有搞错? - 它甚至不会一直解析错误的日期!
更新:这很好地修复了它.你不知道吗,在其他一些地方也被误用了.得爱调试其他人的代码:)
我得到"YYYY-mm-dd hh:mm"格式化对象的日期格式.
如何格式化输入格式化程序对象才能获得"YYYY-mm-dd";?
在使用SimpleDateFormat将字符串解析为日期时,我遇到了一种非常奇怪的行为.考虑以下单元测试:
@Test
public void testParse() throws ParseException
{
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateStr = "2012-12-21";
Date parsedDate = dateFormat.parse(dateStr);
Calendar date = Calendar.getInstance();
date.setTime(parsedDate);
Assert.assertEquals(2012, date.get(Calendar.YEAR));
Assert.assertEquals(11, date.get(Calendar.MONTH)); // yeah, Calendar sucks
Assert.assertEquals(21, date.get(Calendar.DAY_OF_MONTH));
}
Run Code Online (Sandbox Code Playgroud)
可以看出,上面的代码中有一个故意的错误:SimpleDateFormat初始化,"yyyyMMdd"但要解析的字符串是格式"yyyy-MM-dd".我希望这样的事情会导致a ParseException或者至少在正确的基础上正确解析.相反,由于一些奇怪的原因,日期被解析为2011-11-02.嗯?
这是不可接受的,因为处理输入时的一个错误会导致完全意外/毁灭性的事情.在此期间切换到JodaTime,但是理解那里出了什么问题会很好.
好的,所以我有一个字符串,说"Tue May 21 14:32:00 GMT 2012"我想以2012年5月21日下午2:32的格式将此字符串转换为当地时间.我尝试了SimpleDateFormat("MM dd,yyyy hh:mm a").parse(),但它抛出异常.所以我该怎么做?
例外情况是"未报告的异常java.text.ParseException;必须被捕获或声明被抛出".
在线 Date date = inputFormat.parse(inputText);
我在TextMate上运行的代码:
public class test{
public static void main(String arg[]) {
String inputText = "Tue May 22 14:52:00 GMT 2012";
SimpleDateFormat inputFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
SimpleDateFormat out = new SimpleDateFormat("MMM dd, yyyy h:mm a");
Date date = inputFormat.parse(inputText);
String output = out.format(date);
System.out.println(output);
}
}
Run Code Online (Sandbox Code Playgroud) 您好我试图将用户输入的日期(作为字符串)与当前日期进行比较,以便确定日期是更早还是更早.
我目前的代码是
String date;
Date newDate;
Date todayDate, myDate;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
while(true)
{
Scanner s = new Scanner (System.in);
date = s.nextLine();
Calendar cal = Calendar.getInstance();
try {
// trying to parse current date here
// newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception
// trying to parse inputted date here
myDate = dateFormatter.parse(date); //no exception
} catch (ParseException e) {
e.printStackTrace(System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图将用户输入日期和当前日期都放入两个Date对象,以便我可以使用Date.compareTo()来简化比较日期.
我能够将用户输入字符串解析为Date对象.但是,当前日期cal.getTime().toString()由于是无效字符串而不会解析为Date对象.
怎么去做这个?提前致谢
import java.text.ParseException;
public class Hello {
public static void main(String[] args) throws ParseException {
System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd").parse("23-06-2015"));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这会回来Sun Dec 05 00:00:00 GMT 28我期待一个例外.
我一直在调试一些现有的代码,我的系统上的单元测试失败了,但同事的系统却没有.根本原因是SimpleDateFormat在解析应该可解析的日期时抛出ParseExceptions.我创建了一个单元测试,演示了我的系统失败的代码:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import junit.framework.TestCase;
public class FormatsTest extends TestCase {
public void testParse() throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z");
formatter.setTimeZone(TimeZone.getDefault());
formatter.setLenient(false);
formatter.parse(formatter.format(new Date()));
}
}
Run Code Online (Sandbox Code Playgroud)
此测试在我的系统上抛出ParseException,但在其他系统上成功运行.
java.text.ParseException: Unparseable date: "20100603100243.118 -0600"
at java.text.DateFormat.parse(DateFormat.java:352)
at FormatsTest.testParse(FormatsTest.java:16)
Run Code Online (Sandbox Code Playgroud)
我发现我可以setLenient(true),测试会成功.该setLenient(false)是什么样的生产代码,本次测试模拟使用,所以我不想去改变它.
Java 7 SimpleDateFormat通过字符X(而不是大写或小写Z)在ISO 8601格式的类中引入了支持.在Java 6中支持这样的格式需要预处理,因此最好的方法是问题.
这种新格式是Z(大写Z)的超集,另外还有两种变体:
因此,正如人们可以从Java 7文档中SimpleDateFormat看到的那样,以下3种格式现在是有效的(而不仅仅是ZJava 6中涵盖的第二种格式),当然,等效:
正如之前关于支持这种"扩展"时区格式的特殊情况的问题所讨论的那样,始终使用':'作为分隔符,将Java 7功能向后移植到Java 6中的最佳方法是将类子SimpleDateformat类化并覆盖其parse()方法,即:
public Date parse(String date, ParsePosition pos)
{
String iso = ... // Replace the X with a Z timezone string, using a regex
if (iso.length() == date.length())
{
return null; // Not an ISO 8601 date
}
Date parsed …Run Code Online (Sandbox Code Playgroud) 好吧,我正在使用一个详细信息Date,因为我从我的DataBase和变量"fecha"(日期)中得到一个对象我得到的同一个对象java.sql.Timestamp,所以格式是毫秒,但我不希望出现毫秒.所以我需要将我从数据库接收的日期格式化为没有毫秒数的新日期.
这是Factura的对象:
public class Factura implements java.io.Serializable {
private FacturaId id;
...
private boolean activo;
private Date fecha;
}
Run Code Online (Sandbox Code Playgroud)
在映射到DB的xml中,我有这个变量"fecha"的代码:
<property name="fecha" type="timestamp">
<column length="19" name="fecha" not-null="true"/>
</property>
Run Code Online (Sandbox Code Playgroud)
在列的数据库中fecha DATETIME.
当我Factura从我的数据库中获取一个对象时,我得到了这样的约会,2013-10-10 10:49:29.0但我希望它没有.0(毫秒).
我试过这个(factura是Factura对象):
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fechaNueva = null;
String fechaStr = factura.getFecha().toString();
int tamaño = fechaStr.length()-2;
fechaStr = fechaStr.substring(0, tamaño); //I get a string without the miliseconds …Run Code Online (Sandbox Code Playgroud) 我SimleDateFormat喜欢这个
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm");
String date = format.format(Date.parse(payback.creationDate.date));
Run Code Online (Sandbox Code Playgroud)
我给的日期格式是这样的"Jan,23,2014".
现在,我希望分别获得日,月和年.我该如何实现呢?
java ×10
simpledateformat ×10
date ×5
android ×2
calendar ×1
date-parsing ×1
datetime ×1
java-6 ×1
netbeans-7.1 ×1
parsing ×1
regex ×1
timestamp ×1