如果我运行以下程序,它解析引用时间间隔为1秒的两个日期字符串并比较它们:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld4-ld3);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
353
为什么ld4-ld3不1(正如我所期望的那样,在时间上只有一秒钟的差异),但是353?
如果我将日期更改为1秒后的时间:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Run Code Online (Sandbox Code Playgroud)
然后ld4-ld3会1.
Java版本:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04) …Run Code Online (Sandbox Code Playgroud) 将String"2010年1月2日"格式转换为DateJava 格式的最佳方法是什么?
最终,我希望将月份,日期和年份作为整数分解,以便我可以使用
Date date = new Date();
date.setMonth()..
date.setYear()..
date.setDay()..
date.setlong currentTime = date.getTime();
Run Code Online (Sandbox Code Playgroud)
将日期转换为时间.
我想将java.util.Date对象转换为StringJava中的对象.
格式是 2010-05-30 22:15:52
我有一个日期作为字符串,格式如下"04/02/2011 20:27:05".我正在使用Joda-Time库,并希望将其转换为DateTime对象.我做了:
DateTime dt = new DateTime("04/02/2011 20:27:05")
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"
Run Code Online (Sandbox Code Playgroud)
如何将上述日期转换为DateTime对象?
我试图解析这个日期,SimpleDateFormat它不起作用:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String[] args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我用strDate =尝试这个代码"2008-10-14",我有一个肯定的答案.有什么问题?我该如何解析这种格式?
PS.我从a获得了这个日期,jDatePicker没有关于如何修改用户选择日期时获得的日期格式的说明.
我只是想在Java 8中将日期字符串转换为DateTime对象.运行以下行:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20140218' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor:
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Run Code Online (Sandbox Code Playgroud)
语法与此处的建议相同,但我遇到了一个例外.我在用JDK-8u25.
我有一个epoch秒和一个zoneId,方法1.它可以转换为LocalDateTime与系统默认的zoneId,但我找不到方法将第二个转换为LocalDateTime by method2,因为没有ZoneOffset.systemDefault.我认为它是模糊的.
import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset}
val epochSecond = System.currentTimeMillis() / 1000
LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault())//method1
LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.MAX)//method2
Run Code Online (Sandbox Code Playgroud) 我最近搬到Java 8,希望更容易处理本地和分区时间.
但是,在我看来,在解析一个简单的日期时,我面临一个简单的问题.
public static ZonedDateTime convertirAFecha(String fecha) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
ConstantesFechas.FORMATO_DIA).withZone(
obtenerZonaHorariaServidor());
ZonedDateTime resultado = ZonedDateTime.parse(fecha, formatter);
return resultado;
}
Run Code Online (Sandbox Code Playgroud)
就我而言:
所以,这是一个简单的例子.但是,解析会抛出此异常:
java.time.format.DateTimeParseException: Text '15/06/2014' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2014-06-15 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
有小费吗?我一直在尝试解析和使用TemporalAccesor的不同组合,但到目前为止没有任何运气.
最好的祝福
当我这样做
String datum = "20130419233512";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin"));
OffsetDateTime datetime = OffsetDateTime.parse(datum, formatter);
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
java.time.format.DateTimeParseException: Text '20130419233512' could not be parsed:
Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1366407312},ISO,Europe/Berlin resolved
to 2013-04-19T23:35:12 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
如何解析我的日期时间字符串,以便将其解释为始终来自"Europe/Berlin"时区?
我的dao页面正在从两个不同的字段接收日期和时间现在我想知道如何在单个对象中合并这些日期和时间,以便我计算时间差和总时间.我有这个代码合并但它不工作我在这个代码中做错了请帮助.
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2013-01-02");
Date t = new SimpleDateFormat("hh:mm:ss").parse("04:05:06");
LocalDate datePart = new LocalDate(d);
LocalTime timePart = new LocalTime(t);
LocalDateTime dateTime = datePart.toLocalDateTime(timePart);
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)