How to format Joda-Time DateTime to only mm/dd/yyyy?

前端 未结 9 1203
挽巷
挽巷 2020-11-29 15:52

I have a string \"11/15/2013 08:00:00\", I want to format it to \"11/15/2013\", what is the correct DateTimeFormatter pattern?

相关标签:
9条回答
  • 2020-11-29 16:12

    This works

    String x = "22/06/2012";
    String y = "25/10/2014";
    
    String datestart = x;
    String datestop = y;
    
    //DateTimeFormatter format = DateTimeFormat.forPattern("dd/mm/yyyy");
    SimpleDateFormat  format = new SimpleDateFormat("dd/mm/yyyy");
    
    Date d1 = null;
    Date d2 = null;
    
    try {
        d1 =  format.parse(datestart);
        d2 = format.parse(datestop);
    
        DateTime dt1 = new DateTime(d1);
        DateTime dt2 = new DateTime(d2);
    
        //Period
        period = new Period (dt1,dt2);
    
        //calculate days
        int days = Days.daysBetween(dt1, dt2).getDays();
    
    
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-11-29 16:16

    I have a very dumb but working option. if you have the String fullDate = "11/15/2013 08:00:00";

       String finalDate = fullDate.split(" ")[0];
    

    That should work easy and fast. :)

    0 讨论(0)
  • 2020-11-29 16:23

    Note that in JAVA SE 8 a new java.time (JSR-310) package was introduced. This replaces Joda time, Joda users are advised to migrate. For the JAVA SE ≥ 8 way of formatting date and time, see below.

    Joda time

    Create a DateTimeFormatter using DateTimeFormat.forPattern(String)

    Using Joda time you would do it like this:

    String dateTime = "11/15/2013 08:00:00";
    // Format for input
    DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
    // Parsing the date
    DateTime jodatime = dtf.parseDateTime(dateTime);
    // Format for output
    DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
    // Printing the date
    System.out.println(dtfOut.print(jodatime));
    

    Standard Java ≥ 8

    Java 8 introduced a new Date and Time library, making it easier to deal with dates and times. If you want to use standard Java version 8 or beyond, you would use a DateTimeFormatter. Since you don't have a time zone in your String, a java.time.LocalDateTime or a LocalDate, otherwise the time zoned varieties ZonedDateTime and ZonedDate could be used.

    // Format for input
    DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
    // Parsing the date
    LocalDate date = LocalDate.parse(dateTime, inputFormat);
    // Format for output
    DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    // Printing the date
    System.out.println(date.format(outputFormat));
    

    Standard Java < 8

    Before Java 8, you would use the a SimpleDateFormat and java.util.Date

    String dateTime = "11/15/2013 08:00:00";
    // Format for input
    SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    // Parsing the date
    Date date7 = dateParser.parse(dateTime);
    // Format for output
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
    // Printing the date
    System.out.println(dateFormatter.format(date7));
    
    0 讨论(0)
提交回复
热议问题