5/17/2023 0 Comments Calendar formatter![]() DateTimeZone zone = new DateTimeZone( "Asia/Kolkata" ) ĭateTime dateTime = DateTime.now( zone ) ĭateTimeFormatter formatter = ISODateTimeFormat.date() Similar code using the Joda-Time library, the precursor to java.time. String output = formatterOutput.format( zonedDateTime ) ĭump to console… ( "zonedDateTime: " + zonedDateTime ) ZonedDateTime zonedDateTime = ZonedDateTime.now( zoneId ) ĭateTimeFormatter formatterOutput = DateTimeFormatter.ISO_LOCAL_DATE // Caution: The "LOCAL" part means we are losing time zone information, creating ambiguity. Instead of ISO_LOCAL_DATE I would have used ISO_DATE to get this output: +05:30 ZoneId zoneId = ZoneId.of( "Asia/Kolkata" ) You asked for YYYY-MM-DD output so I provided, but I don't recommend it. Creating string output lacking any time zone or offset information is creating ambiguity. So a moment in a new day in India has one date while the same moment in France has “yesterday’s” date. For example, the India time zone is +05:30 (five and a half hours ahead of UTC), while France is only one hour ahead. The day-of-month depends on the time zone. When working with date-time (as opposed to local date), the time zone in critical. The successor to Joda-Time is now built into Java 8 as the new java.time package. The answer by MadProgrammer is correct, especially the tip about Joda-Time. These are actually the same date, represented differently. String formatted = format1.format(cal.getTime()) SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd") The following is maintained for historical purposes (as the original answer) You should be making use of the ThreeTen Backport Java 8+ LocalDateTime ldt = LocalDateTime.now().plusDays(1) ĭateTimeFormatter formmat1 = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH) You are better off formatting the date to the format you want to use (or display). Now before you fire up your IDE and try this, I wouldn't it will only complicate matters. The only way to change it is to override Date and provide your own implementation of Date.toString(). When you use something like (date), Java uses Date.toString() to print the contents. A Java Date is a container for the number of milliseconds since January 1, 1970, 00:00:00 GMT.
0 Comments
Leave a Reply. |