Convert string to date then format the date

后端 未结 8 449
我寻月下人不归
我寻月下人不归 2020-12-03 07:52

I am formatting a string to a date using the code

String start_dt = \'2011-01-01\';

DateFormat formatter = new SimpleDateFormat(\"YYYY-MM-DD\"); 
Date date          


        
相关标签:
8条回答
  • 2020-12-03 08:05

    Currently, i prefer using this methods:

    String data = "Date from Register: ";
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    // Verify that OS.Version is > API 26 (OREO)
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); 
    // Origin format
                LocalDate localDate = LocalDate.parse(capitalModels.get(position).getDataServer(), formatter); // Parse String (from server) to LocalDate
                DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy"); 
    //Output format
                data = "Data de Registro: "+formatter1.format(localDate); // Output
                Toast.makeText(holder.itemView.getContext(), data, Toast.LENGTH_LONG).show();
            }else{ 
    //Same resolutions, just use legacy methods to oldest android OS versions.
                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd",Locale.getDefault());
                try {
                    Date date = format.parse(capitalModels.get(position).getDataServer());
                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
                    data = "Date from Register: "+formatter.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
    
    0 讨论(0)
  • 2020-12-03 08:07

    In one line:

    String date=new SimpleDateFormat("MM-dd-yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse("2011-01-01"));
    

    Where:

    String date=new SimpleDateFormat("FinalFormat").format(new SimpleDateFormat("InitialFormat").parse("StringDate"));
    
    0 讨论(0)
  • 2020-12-03 08:08
    String start_dt = "2011-01-31";
    
    DateFormat parser = new SimpleDateFormat("yyyy-MM-dd"); 
    Date date = (Date) parser.parse(start_dt);
    
    DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
    System.out.println(formatter.format(date));
    

    Prints: 01-31-2011

    0 讨论(0)
  • 2020-12-03 08:14

    String myFormat= "yyyy-MM-dd";
    String finalString = "";
    try {
    DateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
    Date date = (Date) formatter .parse("2015 Oct 09");
    SimpleDateFormat newFormat = new SimpleDateFormat(myFormat);
    finalString= newFormat .format(date );
    newDate.setText(finalString);
    } catch (Exception e) {
    
    }
    
    0 讨论(0)
  • 2020-12-03 08:17
        String start_dt = "2011-01-01"; // Input String
    
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // Existing Pattern
        Date getStartDt = formatter.parse(start_dt); //Returns Date Format according to existing pattern
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy");// New Pattern
        String formattedDate = simpleDateFormat.format(getStartDt); // Format given String to new pattern
    
        System.out.println(formattedDate); //outputs: 01-01-2011
    
    0 讨论(0)
  • 2020-12-03 08:19

    tl;dr

    LocalDate.parse( "2011-01-01" )
             .format( DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ) 
    

    java.time

    The other Answers are now outdated. The troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes.

    ISO 8601

    The input string 2011-01-01 happens to comply with the ISO 8601 standard formats for date-time text. The java.time classes use these standard formats by default when parsing/generating strings. 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( "2011-01-01" ) ;
    

    Generate a String in the same format by calling toString.

    String output = ld.toString() ;
    

    2011-01-01

    DateTimeFormatter

    To parse/generate other formats, use a DateTimeFormatter.

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ;
    String output = ld.format( f ) ;
    

    01-01-2011


    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.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, 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
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • 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)
提交回复
热议问题