首页 文章

如何将Joda-Time DateTime格式化为mm / dd / yyyy?

提问于
浏览
162

我有一个字符串“ 11/15/2013 08:00:00 ", I want to format it to " 11/15/2013 ”,什么是正确的 DateTimeFormatter 模式?

我尝试了很多并用Google搜索,仍然无法找到正确的模式 .

编辑:我正在寻找Joda-Time DateTimeFormatter,而不是Java的SimpleDateFormat ..

8 回答

  • 9

    我有一个非常愚蠢但工作的选择 . 如果你有String fullDate =“11/15/2013 08:00:00”;

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

    这应该简单快捷 . :)

  • 1

    只想要字符串:

    DateTime.parse("201711201515",DateTimeFormat.forPattern("yyyyMMddHHmm")).toString("yyyyMMdd");
    

    如果想要日期时间:

    DateTime.parse("201711201515", DateTimeFormat.forPattern("yyyyMMddHHmm")).withTimeAtStartOfDay();
    
  • 18
    DateTime date = DateTime.now().withTimeAtStartOfDay();
    date.toString("HH:mm:ss")
    
  • 337

    我在这里添加这个,即使其他答案是完全可以接受的 . JodaTime在DateTimeFormat中预先构建了解析器:

    dateTime.toString(DateTimeFormat.longDate());
    

    这是以其格式打印出来的大多数选项:

    shortDate:         11/3/16
    shortDateTime:     11/3/16 4:25 AM
    mediumDate:        Nov 3, 2016
    mediumDateTime:    Nov 3, 2016 4:25:35 AM
    longDate:          November 3, 2016
    longDateTime:      November 3, 2016 4:25:35 AM MDT
    fullDate:          Thursday, November 3, 2016
    fullDateTime:      Thursday, November 3, 2016 4:25:35 AM Mountain Daylight Time
    
  • 33

    如果您使用的是JodaTime,我认为这会有效:

    String strDateTime = "11/15/2013 08:00:00";
    DateTime dateTime = DateTime.parse(strDateTime);
    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/YYYY");
    String strDateOnly = fmt.print(dateTime);
    

    我从here得到了一部分 .

  • 1

    另一种方法是:

    String date = dateAndTime.substring(0, dateAndTime.indexOf(" "));
    

    我不确定,但我认为这可能比使用 .split() 方法更快/使用更少的内存 .

  • 10

    这很有效

    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();
    }
    
  • 1

    乔达时间

    Create a DateTimeFormatter using DateTimeFormat.forPattern(String)

    使用Joda时间你会这样做:

    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));
    

    标准Java≥8

    Java 8引入了new Date and Time library,使得处理日期和时间变得更加容易 . 如果要使用标准Java版本8或更高版本,则应使用DateTimeFormatter . 由于您的 String 中没有时区,java.time.LocalDateTimeLocalDate,否则可以使用时区分区ZonedDateTimeZonedDate .

    // 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));
    

    标准Java <8

    在Java 8之前,你将使用a SimpleDateFormatjava.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));
    

相关问题