DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
sdf.parse("Sun Dec 13 10:00:00 UTC 2009")
Run Code Online (Sandbox Code Playgroud)
结果
java.text.ParseException:Unparseable date:Sun Dec 13 10:00:00 UTC 2009
注意:此代码似乎适用于普通的Java应用程序,但似乎在Android上失败.
我正在尝试将以下字符串"2012-04-13 04:08:42.794"转换为日期类型:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date convertedDate;
try {
convertedDate = dateFormat.parse(dateString);
System.out.println(" in utils: "+convertedDate.toString());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
//----------------- i think this is the problem
java.sql.Date sqlDate = new java.sql.Date(convertedDate.getTime());
System.out.println("sql: "+sqlDate.toString());
return sqlDate;
Run Code Online (Sandbox Code Playgroud)
但这是打印以下内容:
in utils: Fri Apr 13 04:08:42 PDT 2012
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到这个日期以保持毫秒?
我正在开发一个spring应用程序,在我的一个控制器中,我有以下行从字符串到日期进行解析,并将解析的日期格式化为所需的格式.但是我需要在没有使用任何SimpleDateFormat的情况下将格式化的字符串解析回日期,所以有可能这样做吗?
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
Date pick=dateFormat.parse(request.getParameter("pickDate"));
String pick_date=dateFormat2.format(pick);
Run Code Online (Sandbox Code Playgroud) 如何使用一些DateFormat解析这样的字符串?
7月24日星期三
据我所知,没有任何内容SimpleDateFormat.
我有一个字符串,可能包含以下任何格式的日期:
2001-01-05 (yyyy-mm-dd)
2001/01/05 (yyyy/mm/dd)
01/05/2001 (dd/mm/yyyy)
01-05-2001 (dd-mm-yyyy)
2001 january 5
2001 5 january
january 5 2001
5 january 2001
january 5
5 january
Run Code Online (Sandbox Code Playgroud)
我希望能够解析特定的字符串并从中提取Date对象.
我的方法如下:
String[] date_formats = {
"yyyy-MM-dd",
"yyyyy/MM/dd",
"dd/MM/yyyyy",
"dd-MM-yyyy",
"yyyy MMM dd",
"yyyy dd MMM",
"dd MMM yyyy",
"dd MMM",
"MMM dd",
"dd MMM yyyy"};
String output_date = null;
for (String formatString : date_formats)
{
try
{
Date mydate = new SimpleDateFormat(formatString).parse(token);
SimpleDateFormat outdate = new SimpleDateFormat("yyyyMMdd");
output_date = outdate.format(mydate);
break;
}
catch (ParseException …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么SimpleDateFormat在使用SimpleTimeZone设置时区时将我的解析日期减去1秒?
这是一个jdk bug吗?
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
public class SimpleDateFormatTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(format.parse("2016-02-24T17:31:00Z"));
// prints Wed Feb 24 17:31:00 UTC 2016
format.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
System.out.println(format.parse("2016-02-24T17:31:00Z"));
// Wed Feb 24 17:30:59 UTC 2016
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用这个功能,但它不适用于这种情况'12/05/201a'有人知道为什么会发生这种情况?
在我的测试中,我使用了这个 System.out.println(isThisDateValid("12/05/201a", "dd/MM/yyyy"));,答案是,true但我预计结果将是假的,因为年份包含字母.
public static boolean isThisDateValid(String dateToValidate, String dateFromat)
{
if (dateToValidate == null)
{
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
sdf.setLenient(false);
try
{
//if not valid, it will throw ParseException
Date date = sdf.parse(dateToValidate);
System.out.println(date);
} catch (ParseException e)
{
e.printStackTrace();
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud) SimpleDateFormat过去几年我一直在成功使用。我使用它构建了一堆时间实用程序类。
由于我遇到了SimpleDateFormat(SDF) 不是线程安全的问题,因此我在过去几天中将这些实用程序类重构为DateTimeFormatter现在内部使用(DTF)。由于两个班级的时间模式几乎相同,因此这种转变在当时似乎是个好主意。
我现在在获取EpochMillis(自 以来的毫秒数1970-01-01T00:00:00Z)时遇到问题:虽然 SDF 会例如解释10:30使用HH:mmas解析1970-01-01T10:30:00Z,但 DTF 不会做同样的事情。DTF可以使用10:30来解析LocalTime,但不是ZonedDateTime它需要获得EpochMillis。
我理解对象java.time遵循不同的哲学;Date、Time和Zoned对象分开保存。但是,为了让我的实用程序类像以前一样解释所有字符串,我需要能够为所有丢失的对象动态定义默认解析。我试着用
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.parseDefaulting(ChronoField.YEAR, 1970);
builder.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1);
builder.parseDefaulting(ChronoField.DAY_OF_MONTH, 1);
builder.parseDefaulting(ChronoField.HOUR_OF_DAY, 0);
builder.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0);
builder.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0);
builder.append(DateTimeFormatter.ofPattern(pattern));
Run Code Online (Sandbox Code Playgroud)
但这并不适用于所有模式。它似乎只允许未在pattern. 有没有办法测试ChronoField定义了哪些spattern然后有选择地添加默认值?
或者,我试过
TemporalAccessor temporal = formatter.parseBest(time,
ZonedDateTime::from,
LocalDateTime::from,
LocalDate::from,
LocalTime::from,
YearMonth::from,
Year::from,
Month::from);
if …Run Code Online (Sandbox Code Playgroud) 我使用此代码收到此错误:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd MMMM HH:mm yyyy",myDateFormatSymbols);
sdf.parse("????????? 12 ??????? 07:00 2021");
Run Code Online (Sandbox Code Playgroud)
是
"Monday 12 April 07:00 2021"。问题是,每当我将日期从星期一更改为星期二 ( "????????") 时,我都不会收到此错误,并且代码有效。这是代码myDateFormatSymbols:
private final static DateFormatSymbols myDateFormatSymbols = new DateFormatSymbols(){
@Override
public String[] getWeekdays(){
return new String[]{"?????????","????????", "??????", "??????", "???????", "??????", "??????"};
}
@Override
public String[] getMonths() {
return new String[]{...};
}
}
Run Code Online (Sandbox Code Playgroud)
所有月份和工作日都正常工作,似乎这个错误只发生在星期一。
我有两种日期格式:
"yyyy-MM-dd", "dd-MM-yyyy"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试解析日期 : 时"11-05-2021",它不会抛出"yyyy-MM-dd"格式异常。以下是我正在尝试做的事情:
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
sdf.parse(d);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
为什么它没有给出"yyyy-MM-dd"format的例外。如何区分这两种格式?
谢谢
java ×10
simpledateformat ×10
date ×4
android ×1
date-format ×1
date-parsing ×1
datetime ×1
dayofweek ×1
java-time ×1
parsing ×1