Tag: date

基于区域设置的Joda-Time DateTime格式

我从另一个系统收到一个字符串日期,我知道该日期的区域设置(也可从其他系统获得)。 我想将此String转换为Joda-Time DateTime对象,而不显式指定目标模式。 因此,例如,我想将此字符串“09/29/2014”仅使用区域设置转换为日期对象,而不是将日期格式硬编码为“mm / dd / yyyy”。 我不能对格式进行硬编码,因为这会根据我收到的日期的当地情况而有所不同。

如何制作具有固定日期格式的jtextfield?

我在使用JTextField创建固定日期格式时遇到问题。 JTextField有没有办法使用固定的日期格式?

为什么在1582之前将Java日期转换为使用Instant的LocalDate给出不同的日期?

考虑以下代码: Date date = new SimpleDateFormat(“MMddyyyy”).parse(“01011500”); LocalDate localDateRight = LocalDate.parse(formatter.format(date), dateFormatter); LocalDate localDateWrong = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(); System.out.println(date); // Wed Jan 01 00:00:00 EST 1500 System.out.println(localDateRight); // 1500-01-01 System.out.println(localDateWrong); // 1500-01-10 我知道1582年是Julian和Gregorian日历之间的截止点。 我不知道为什么会发生这种情况,或者如何调整它。 这是我到目前为止所发现的: 日期对象的BaseCalender设置为JulianCalendar date.toInstant()只返回Instant.ofEpochMilli(getTime()) date.getTime()返回-14830974000000 -14830974000000是Wed,10 Jan 1500 05:00:00 GMT Gregorian 所以似乎getTime()返回的millis是错误的(不太可能)或者只是与我预期的不同而且我需要考虑差异。

如何使用默认区域解析ZonedDateTime?

如何从不包含zone和其他字段的字符串解析ZoneDateTime ? 这是在Spock中重现的测试: import spock.lang.Specification import spock.lang.Unroll import java.time.ZoneId import java.time.ZoneOffset import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @Unroll class ZonedDateTimeParsingSpec extends Specification { def “DateTimeFormatter.ISO_ZONED_DATE_TIME parsing incomplete date: #value #expected”() { expect: ZonedDateTime.parse(value, DateTimeFormatter.ISO_ZONED_DATE_TIME) == expected where: value | expected ‘2014-04-23T04:30:45.123Z’ | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.UTC) ‘2014-04-23T04:30:45.123+01:00’ | ZonedDateTime.of(2014, 4, 23, 4, 30, […]

如何将System.nanoTime的结果转换为Java中的日期?

我想将System.nanoTime()的结果转换为日期。 public void tempBan(Player p, Player banner, int timeInSeconds){ Long timeInNano = (long) (timeInSeconds * 10^9); int newTime = (int) (System.nanoTime() + timeInNano); // here I want to convert newTime to a date } 我已经通过乘以10 ^ 9将秒转换为纳秒 。 现在我需要将当前系统时间加上我转换为纳秒的参数转换为日期。

使用Java静态方法中的“today”和“yesterday”字符串简化日期对象的替换

我有以下方法,我想更短或更快,如果没有别的。 欢迎所有评论: Bellow方法接受一个日期对象,形成它(“EEE hh:mma MMM d,yyyy”),然后确定日期是今天还是昨天,如果是,则返回“(昨天|今天)hh:mma “格式化的字符串。 public static String formatToYesterdayOrToday(String date) { SimpleDateFormat sdf = new SimpleDateFormat(“EEE hh:mma MMM d, yyyy”); Date in = null; try { in = sdf.parse(date); } catch (ParseException e) { log.debug(“Date parsing error:”, e); } Calendar x = Calendar.getInstance(); x.setTime(in); String hour = Integer.toString(x.get(Calendar.HOUR)); String minute = Integer.toString(x.get(Calendar.MINUTE)); String […]

java.text.ParseException:无法解析的日期:将mm / dd / yyyy字符串转换为日期

当我将我的字符串对象以mm/dd/yyyy格式转换为Date它给了我 java.text.ParseException: Unparseable date: “09/17/2014” 我想这样做: String date= “09/17/2014”; DateFormat df = new SimpleDateFormat(); Date journeyDate= (java.sql.Date) df.parse(date);

用Java获取当前的开始和结束日期 – (周一至周日)

今天是2014-04-06(星期日)。 我使用以下代码得到的输出是: Start Date = 2014-04-07 End Date = 2014-04-13 这是我想要的输出: Start Date = 2014-03-31 End Date = 2014-04-06 我怎样才能做到这一点? 这是我到目前为止完成的代码: // Get calendar set to current date and time Calendar c = GregorianCalendar.getInstance(); System.out.println(“Current week = ” + Calendar.DAY_OF_WEEK); // Set the calendar to monday of the current week c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(“Current week = […]

将TZ格式的字符串转换为Date

可能的解决方案: 将Java Date转换为另一个Time as Date格式 我经历过但没有得到答案。 我有一个字符串“ 2013-07-17T03:58:00.000Z ”,我想将它转换为我们在制作新Date()时获得的相同forms的日期。日期d =新Date(); 时间应该在IST.Zone-Asia / Kolkata 因此,上面字符串的日期应该是 7月17日星期三12:05:16 IST 2013 //根据印度标准GMT + 0530的时间 String s=”2013-07-17T03:58:00.000Z”; DateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”); TimeZone tx=TimeZone.getTimeZone(“Asia/Kolkata”); formatter.setTimeZone(tx); d= (Date)formatter.parse(s);

用Joda和时区解析约会

我有两个时间戳,以两种不同的格式描述相同的时刻。 2010-10-03 18:58:07和2010-10-03T16:58:07.000+02:00 我用Joda用两个不同的日期格式化器解析时间戳。 最后,我希望有两个DateTime对象,它们在同一时刻是相等的。 DateFormatter提供了几种控制时区和语言环境的方法,但我无法使其工作。 这是我想要的代码: final String date1 = “2010-10-03 18:58:07”; // Europe/Berlin local time final DateTimeFormatter formatter1 = DateTimeFormat.forPattern(“yyyy-MM-dd HH:mm:ss”); final DateTime dateTime1 = formatter1.parseDateTime(date1); final String date2 = “2010-10-03T16:58:07.000+02:00”; // Europe/Berlin local time with time zone final DateTimeFormatter formatter2 = DateTimeFormat.forPattern(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”); final DateTime dateTime2 = formatter2.parseDateTime(date2); Assert.assertTrue(dateTime1.isEqual(dateTime2)); 如果有人可以帮助我,请提前感谢!