Tag: datetime

使用Java 8 java.time api解析ISO时间戳(仅限标准版)

我在例子中从字符串中获取毫秒数时遇到麻烦。 到目前为止,我已尝试过这三种不同的方式,示例显示了最新的尝试。 似乎总是可以归结为TemporalAccessor不支持ChronoField 。 如果我能成功构建一个Instant实例,我可以使用toEpochMilli() 。 String dateStr = “2014-08-16T05:03:45-05:00” TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr); Instant creationDate = Instant.from(creationAccessor); 请提供简明的答案(不要从头开始构建格式化程序)并仅使用java 8标准发行版(我可以使用Joda,但希望避免依赖项)。 编辑:上面代码中的java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=-18000},ISO resolved to 2014-08-16T05:03:45 of type java.time.format.Parsed抛出: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=-18000},ISO resolved to 2014-08-16T05:03:45 of type java.time.format.Parsed

如何将String时间戳转换为java.sql.Timestamp?

我有时间戳的字符串格式为“星期四,2017年2月23日04:56:38 GMT”。 那么,如何将此字符串时间戳转换为“2017-02-23 04:56:38.0”格式。 我尝试将字符串格式的java转换日期链接到时间戳 。 但面临转换错误。 我的示例代码如下。 String str = “Thu, 23 Feb 2017 04:56:38 GMT”; SimpleDateFormat sdf1 = new SimpleDateFormat(“EEE, dd MMM yyyy hh:mm:ss a”); Date date = null; try { date = (Date) sdf1.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(“Date Object:” + date); SimpleDateFormat sdf2 […]

如何在Java中将字符串转换为Time对象?

我的字符串值为08:03:10 pm ,我想把它转换成时间 。 我怎么能用Java做到这一点?

以30分钟(最近的半小时)递增:

以30为增量舍入整数的最有效方法(在Java中)是什么。 我的输出将是一个整数: 这是我的代码,它以整数forms返回分钟。 long different = endTime.getTime() – startTime.getTime(); int idiff = TimeUnit.MILLISECONDS.toMinutes(different) – TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(different)); 现在我想整理这个整数。 例如: 任何数字<= 30 =最多30个: 31到60之间的任何数字=最多60个 61到90之间的任何数字=最多90个 91到120之间的任何数字=最多120个 任何数字在121到150之间=最多150

将Java Date从一种格式转换为另一种格式而不将其转换为字符串

我正在尝试获取当前的系统日期,并将其格式化为Etc / UTC,然后进入此“dd.MM.yyyy HH:mm z”格式。 问题是格式化日期后格式化函数返回一个字符串而不是日期。 这是下面的代码spinet final Date currentDate = new Date(); final SimpleDateFormat dateFormatter = new SimpleDateFormat(“dd.MM.yyyy HH:mm z”); dateFormatter.setTimeZone(TimeZone.getTimeZone(“Etc/UTC”)); String finalDateTime= dateFormatter.format(currentDate); System.out.println(finalDateTime); 是否有任何替代解决方案允许我通过将我保留在此日期对象中来格式化日期,因为我已经在java 8之后和java 8之前研究了每个日期库,看起来如果我想格式化任何类型的日期或日期时间,我必须使用格式化程序将给定日期转换为字符串。 任何替代解决方案或不可能吗?

DateTimeFormatter基于周的年份差异

我正在将我的应用程序从Joda-Time迁移到Java 8 java.time 。 我遇到的一件事是使用DateTimeFormatter的模式打印基于周的年份。 注意:我已经看到了这个问题: Java Time使用DateTimeFormatter解析基于星期的周模式 根据文件 y year-of-era year 2004; 04 Y week-based-year year 1996; 96 然而,当我尝试这两个时,似乎Y总是和y一样返回。 我的测试代码: DateTimeFormatter yearF = DateTimeFormatter.ofPattern(“yyyy”).withZone(ZoneOffset.UTC); DateTimeFormatter weekYearF = DateTimeFormatter.ofPattern(“YYYY”).withZone(ZoneOffset.UTC); DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR_OF_ERA) .appendLiteral(” “) .append(yearF) .appendLiteral(” — “) .appendValue(IsoFields.WEEK_BASED_YEAR) .appendLiteral(” “) .append(weekYearF) .toFormatter() .withZone(ZoneOffset.UTC); System.out.println(dateTimeFormatter.toString()); ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(946778645000L), ZoneOffset.UTC); for (int […]

Java将String转换为日期然后比较

我有一个2D字符串数组 static String[][] TaskStorage = { {“Task 1″,”02/07/13”}, {“Task 2″,”02/08/13”}, {“Task 3″,”02/09/13”}, {“Task 4″,”02/10/13”}, {“Task 5″,”02/11/13”}, {“Task 6″,”02/03/13”}, {“Task 7″,”02/04/13”}, {“Task 8″,”02/05/13”}, {“Task 9″,”02/06/13”}, {“Task 10″,”03/01/13”}, {“Task 11″,”03/02/13”}, {“Task 12″,”03/03/13”}, }; 我想打印所有TaskStorage [0] [i],其中TaskStorage [i] [1]是02/07和02/08。 考虑到今天的日期是02/04。 今天的日期可以改为02/28。 todayDate将基于代码的执行时间。 基本上打印任务提前两天。 这就是我尝试过的: SimpleDateFormat dateFormat = new SimpleDateFormat(“mm/dd/yy”); TaskDate = dateFormat.parse(LinkStorage[i][1]); if (todayDate.after(TaskDate)) { System.out.println(“”); }

日期exception不正确

我试图使用java进入sql进入1900年的日期,但我得到例外: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: ‘1900-01-01 00:00:00’

如何从String获取Date对象

DateFormat formatter = new SimpleDateFormat(“MM/dd/yyyy’ ‘HH:mm:ss”); Date d = (Date)formatter.parse(dateTime); System.out.println(“date in controller “+d); 我把输出作为 控制日期:2012年12月31日16:04:57 IST 2012 请建议以MM/dd/yyyy HH:mm:ss格式输出日期的方法。 需要以日期格式输出而不是字符串,以便将输出存储在mysql db中的datetime字段中

使用Java 8 DateTimeFormatter和西class牙语月份名称进行解析

使用’old’,Java 8之前的SimpleDateFormat我可以做到: new java.text.SimpleDateFormat(“MMM yyyy”, new java.util.Locale(“es”, “ES”)).parse(“Mayo 2017”) 获取具有西class牙月份名称的日期的Date对象。 如何使用Java 8和DateTimeFormatter实现相同的function? 我试过了: DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(new Locale(“es”, “ES”)).ofPattern(“MMM yyyy”).parse(“Mayo 2017”) 但只获得java.time.format.DateTimeParseException 。