Parse String date in (yyyy-MM-dd) format

后端 未结 5 710
小蘑菇
小蘑菇 2020-11-30 14:51

I have a string in the form \"2013-09-18\". I want to convert it into a java.util.Date. I am doing this

SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM         


        
相关标签:
5条回答
  • 2020-11-30 15:19

    I convert String to Date in format ("yyyy-MM-dd") to save into Mysql data base .

     String date ="2016-05-01";
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
     Date parsed = format.parse(date);
     java.sql.Date sql = new java.sql.Date(parsed.getTime());
    

    sql it's my output in date format

    0 讨论(0)
  • 2020-11-30 15:28

    tl;dr

    LocalDate.parse( "2013-09-18" )
    

    … and …

    myLocalDate.toString()  // Example: 2013-09-18
    

    java.time

    The Question and other Answers are out-of-date. The troublesome old legacy date-time classes are now supplanted by the java.time classes.

    ISO 8601

    Your input string happens to comply with standard ISO 8601 format, YYYY-MM-DD. The java.time classes use ISO 8601 formats by default when parsing and generating string representations of date-time values. So no need to specify a formatting pattern.

    LocalDate

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    LocalDate ld = LocalDate.parse( "2013-09-18" );
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
  • 2020-11-30 15:32
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    String cunvertCurrentDate="06/09/2015";
    Date date = new Date();
    date = df.parse(cunvertCurrentDate);
    
    0 讨论(0)
  • 2020-11-30 15:35

    You are creating a Date object, which is a representation of a certain point in the timeline. This means that it will have all the parts necessary to represent it correctly, including minutes and seconds and so on. Because you initialize it from a string containing only a part of the date, the missing data will be defaulted.

    I assume you are then "printing" this Date object, but without actually specifying a format like you did when parsing it. Use the same SimpleDateFormat but call the reverse method, format(Date) as Holger suggested

    0 讨论(0)
  • 2020-11-30 15:42

    You may need to format the out put as follows.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date convertedCurrentDate = sdf.parse("2013-09-18");
    String date=sdf.format(convertedCurrentDate );
    System.out.println(date);
    

    Use

    String convertedCurrentDate =sdf.format(sdf.parse("2013-09-18"));
    

    Output:

    2013-09-18
    
    0 讨论(0)
提交回复
热议问题