logo
down
shadow

DateTimeFormatter and SimpleDateFormat produce different strings


DateTimeFormatter and SimpleDateFormat produce different strings

By : Sid joshi
Date : October 17 2020, 06:10 AM
help you fix your problem According to https://www.timeanddate.com/time/change/usa/new-york?year=1883 (which was the first hit in a Google search for "1883 time adjustment"):
Nov 18, 1883 - Time Zone Change (LMT → EST)
code :


Share : facebook icon twitter icon
Problems when moving from SimpleDateFormat to DateTimeFormatter

Problems when moving from SimpleDateFormat to DateTimeFormatter


By : ed gazmendo
Date : March 29 2020, 07:55 AM
wish of those help Java-8-solution:
Change the order of your parsing instructions inside the builder such that the defaulting instructions all happen AFTER the pattern instruction.
code :
private static final DateTimeFormatter FLEXIBLE_FORMATTER;

static {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.appendPattern("MM/dd");
    builder.parseDefaulting(ChronoField.YEAR_OF_ERA, 1970);
    builder.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1);
    builder.parseDefaulting(ChronoField.DAY_OF_MONTH, 1);
    builder.parseDefaulting(ChronoField.HOUR_OF_DAY, 0);
    builder.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0);
    builder.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0);
    FLEXIBLE_FORMATTER = builder.toFormatter();
}
Convert SimpleDateFormat to DateTimeFormatter

Convert SimpleDateFormat to DateTimeFormatter


By : SivaS
Date : March 29 2020, 07:55 AM
To fix this issue So there may be other answers to this but what I came up caters for the most extreme case I have. Firstly I reduced dd/MM to d/M. This denotes the minimum number of expected characters so will parse double digits completely fine. Note you could also use new DateTimeFormatterBuilder().parseLenient() but this seemed unnecessary.
Secondly I decided to use the optional clause in the format pattern itself. This allows you to specify which parts may not be provided which is exactly the case I was trying to solve.
code :
DateTimeFormatter.ofPattern("d/M/yyyy[' ']['T'][H:mm[:ss[.S]]][X]");
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy[' ']['T'][H:mm[:ss[.S]]][X]");

public LocalDate getRobustLocalDate(String value) {
    try {
        return LocalDate.parse(value, formatter);
    } catch (DateTimeParseException e) {
        return null;
    }
}

@Test
public void testDates() {
    getRobustLocalDate("03/07/2016");               // 2016-07-03
    getRobustLocalDate("3/7/2016");                 // 2016-07-03
    getRobustLocalDate("3/7/2016 00:00:00");        // 2016-07-03
    getRobustLocalDate("3/7/2016 00:00:00.0+0100"); // 2016-07-03
    getRobustLocalDate("3/7/2016T00:00:00.0+0100"); // 2016-07-03
}
Is DateTimeFormatter more strict than SimpleDateFormat? Parsing date with milliseconds

Is DateTimeFormatter more strict than SimpleDateFormat? Parsing date with milliseconds


By : Lea
Date : March 29 2020, 07:55 AM
help you fix your problem SimpleDateFormat's is not strict per default, because the property lenient is true per default. But you can set the property lenient to false to make it strict.
code :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.setLenient(false);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
try {
    System.out.println("SFD: " + simpleDateFormat.parse(date));
} catch (Exception e) {
    System.err.println("SDF failed");
}
try {
    System.out.println("DTF: " + dateTimeFormatter.parse(date));
} catch (Exception e) {
    System.err.println("DTF failed");
}
SDF failed
DTF failed
DateTimeFormatter unable to parse a Date String but SimpleDateFormat is able to

DateTimeFormatter unable to parse a Date String but SimpleDateFormat is able to


By : user3559855
Date : March 29 2020, 07:55 AM
I wish did fix the issue. DateTimeFormatterBuilder.appendValue(TemporalField, int)
I was surprised to find out that this is possible (it doesn’t make much sense to me).
code :
    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
            .appendValue(ChronoField.DAY_OF_MONTH, 1)
            .appendValue(ChronoField.MONTH_OF_YEAR, 1)
            .appendValue(ChronoField.YEAR, 4)
            .toFormatter();

    String dateOfBirth = "312015";
    LocalDate dateTime = LocalDate.parse(dateOfBirth, dateFormatter);
    System.out.println(dateTime);
DateTimeFormatter not compatible with SimpleDateFormat

DateTimeFormatter not compatible with SimpleDateFormat


By : Supriyo
Date : March 29 2020, 07:55 AM
seems to work fine The format you are seeing "ET" is known as the "Generic non-location" format in Unicode Locale Data Markup Language (LDML). The format you want, "EDT", is known as the "Specific non-location" format.
The relevant source code in DateTimeFormatterBuilder checks to see if the date-time object can supply ChronoField.INSTANT_SECONDS. If it can, then the "specific non-location" format is used, if not then the "generic non-location" format is used.
Related Posts Related Posts :
  • Static value in for loop
  • Difference between Class c; and Class c=new Class();
  • My question is about Regular Expression in java
  • Why can't I write ch=ch+1; instead of ch++; though they have same meaning
  • Why is this loop changed?
  • Java (Read and write to file)
  • What's ideal debugging setup? window placement etc
  • Java app behind proxy to use http_proxy variable in linux
  • Straight Java/Groovy versus ETL tool (Talend/etc) - what libraries would you use?
  • Performance testing Flex applications
  • How to insert into two different tables in one statement with Java and MySQL?
  • Object Serialization for Undo/Redo Functionality Java
  • How to retrieve the last image from an SDcard in Android?
  • What's the fastest way to draw a Hello World in Java
  • Java error on bilinear interpolation of 16 bit data
  • Java equivalent for database schema changes like South for Django?
  • Java Robot key activity seems to stop working while certain software is running
  • Will using multiple version of a jar in an application cause problems?
  • JSF RuntimeException: Cannot find FacesContext
  • How do i get a particular day from Jan to Dec
  • Hibernate configuration for Mysql 8.0
  • What JVM can run on CPU and GPU?
  • How to cast an image received from a webservice in Binary64 format to actual image?
  • Is there anyway to get the generated keys when using Spring JDBC batchUpdate?
  • How good is Oracle Universal Connection Pool (UCP)
  • How to populate Java (web) application with initial data using Spring/JPA/Hibernate
  • How do I make my frames change in java?
  • Java Swing - Background of a JPanel
  • Printing pdf from extjs - best solution
  • Clean a RandomAccessFile
  • JFileChooser and browsing networked machines
  • How to write Java string literals that contain double-quotes (")?
  • What do I use Artifacts in IntelliJ IDEA for?
  • Bug with Spring Security 3.0.2 x Spring 3.0.1 - Proxies not being created
  • DBunit generates java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB when trying to load a
  • Hibernate configuration - session factory scanning?
  • Enter Key Press behave like Submit in JSF
  • Using protocol buffers for binary logging
  • EasyMock Class Extension mock isn't mocking methods
  • How do I convert a byte array to Base64 in Java?
  • How to modify camel Exchange and send it
  • How do I avoid constructors?
  • Passing string in where clause using PreparedStatement
  • Tomcat 7 - Ignoring db conections pool parameters (DBCP)
  • How does reduce() method work with parallel streams in Java 8?
  • How can I use a generic class with wildcard declaration?
  • Unable to add ArrayList with multiple arguments
  • Move application into second window monitor in javafx
  • does intern() method have implication on performance?
  • How to run NPM Command in Java code using ProcessBuilder
  • Sending HTTP/2 request via Socket in Java
  • eclipse IDE wrong warning that unused field
  • Decode Java enum from single char
  • How to hide embedded dependencies when using an OSGI bundle with maven?
  • How to correctly implement a generic interface in Java
  • send alert to html page from servlet
  • <EOF>Exception while creating Derby table
  • What does stack.pop() return?
  • Problem with Java Language Levels in IntelliJ IDEA
  • How do i get the status code for a response i subscribe to using the JDK's HttpClient?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk