Tag: datetime format

如何将当前日期时间的格式设置为不同的格式?

我使用以下代码检索当前日期和时间,然后使用以下方法将其添加到表中。 Change 2013-07-29 23:20:34.0 to 29th July 2013, 11:20 PM 约会时间 public Date getCurrentDateTime() { SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); Date date = new Date(); System.out.println(“current:” + dateFormat.format(date)); return date; } 过冬 @Temporal(javax.persistence.TemporalType.TIMESTAMP) public Date getCurrentDateTime() { return date; }

如何用可选部分解析日期时间?

我的日期时间可能是以下格式之一: MM / DD / YY M / DD / YY MM / d / yy的 M / d / yy的 任何上述HH:mm 任何上述4位数的年份 我构建的DateTimeFormatter如下: new DateTimeFormatterBuilder() .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NEVER) .appendLiteral(‘/’) .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, SignStyle.NEVER) .appendLiteral(‘/’) .appendValue(ChronoField.YEAR_OF_ERA, 2, 4, SignStyle.NEVER) .optionalStart() .appendLiteral(‘ ‘) .appendValue(HOUR_OF_DAY, 2) .appendLiteral(‘:’) .appendValue(MINUTE_OF_HOUR, 2) .toFormatter(); 但它无法格式化2/9/17 。 为什么?

如何使用Gson序列化和反序列化Java 8的java.time类型?

我正在使用GSON将一些对象图序列化为JSON。 这些对象图使用新的Java 8 java.time实体( ZonedDateTime , OffsetDateTime , OffsetDateTime等)。 我在这里找到了一个Joda Time序列化器库 – 是否有一个JDK java.time类的等效库? ( 这个人在使用带有java.time没有运气 – 他们的问题仍然没有答案)。

如何将getTime()转换为’YYYY-MM-DD HH24:MI:SS’

我在我的应用程序中有以下代码 System.out.println(rec.getDateTrami().getTime()); 我需要转换以下格式(我想它们是秒) 43782000 29382000 29382000 格式YYYY-MM-DD HH24:MI:SS ,有人可以帮我吗?

如何指定在Joda-Time中应该解析哪个世纪的日期

我有一些HR记录,其日期格式为dd/MM/yy ,我使用Joda-Time将它们标准化为dd-MM-yyyy 。 因此,例如,以下记录标准化如下 Input Output 30/01/14 –> 30-01-2014 15/07/99 –> 15-07-1999 24/03/84 –> 24-03-1984 根据各种标准(人类寿命的平均长度,公司一直存在的时间……),我可以假设99可能会参考。 但是,如果我想指定99指1899或其他一年结束于99 ,我该怎么做? 我正在阅读DateTimeFormatter模式的文档,以及这里的解释,看起来CenturyOfEra字段C可能是我想要使用的,但我不会明白我将如何使用它。

从String转换为Date throws无法解析的日期exception

我想将Date转换为String但我遇到了一些问题。 我的代码是这样的: SimpleDateFormat formato = new SimpleDateFormat( “EEE MMM dd HH:mm:ss z yyyy”); String hacer = “Fri Nov 01 10:30:02 PDT 2013”; Date test = null; test = formato.parse( hacer); System.out.println(“prueba===>” + test); 但没有什么是错的,eclipse向我展示了这个错误: Unparseable date: “Fri Nov 01 10:30:02 PDT 2013” at java.text.DateFormat.parse(Unknown Source) 一些帮助?

在Java中将秒数转换为HH:MM(无秒)

我知道您可以使用DateUtils.formatElapsedTime(seconds)将数秒转换为格式为HH:MM:SS的String 。 但有没有任何实用function可以让我执行相同的转换,但没有秒? 例如,我想将3665秒转换为1:01 ,即使它正好是1:01:05 。 换句话说,只需删除秒部分即可。 强烈倾向于指向效用函数(如果存在)的答案而不是一堆本地滚动算法。

获取原始模式字符串给定JDK 8 DateTimeFormatter?

与我的问题相关 – 如何获得给定DateTimeFormatter的原始pattern字符串?

尝试解析LocalDateTime时出现exception

我使用以下时间戳格式: yyyyMMddHHmmssSSS 以下方法工作正常: public static String formatTimestamp(final Timestamp timestamp, final String format) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return timestamp.toLocalDateTime().format(formatter); } 并且,当我使用该格式字符串传入时间戳时,它会返回,例如: 20170925142051591 然后我需要再次从该字符串映射到时间戳,基本上是反向操作。 我知道我可以使用SimpleDateFormat及其parse()方法,但如果可能的话,我更愿意坚持使用java.time样式格式。 我写了这个(相当hacky)的一些代码,它适用于某些格式,但不适用于这个特定的格式: public static Timestamp getTimestamp(final String text, final String format, final boolean includeTime) { final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); final TemporalAccessor temporalAccessor = formatter.parse(text); if (includeTime) { final LocalDateTime localDateTime […]

Java日期更改格式

我试图更改Date对象的格式,我试图这样做: for(Date date : dates){ DateFormat formatter = new SimpleDateFormat(“yyyy/MM/dd”); String formatterDate = formatter.format(date); Date d = formatter.parse(formatter.format(date)); } 但是这对d对象没有任何影响,它仍然采用旧格式,无法真正理解为什么会这样。