代码之家  ›  专栏  ›  技术社区  ›  Jason

将日历日期转换为字符串?

  •  1
  • Jason  · 技术社区  · 16 年前

            // Set calendar for due date on invoice gui
        Calendar cal = Calendar.getInstance();
    
        // Add 30 days to the calendar for the due date
        cal.add(Calendar.DATE, 30);
        Date dueDate = cal.getTime();
        dueDatestr = Calendar.toString(dueDate);
    
    4 回复  |  直到 16 年前
        1
  •  3
  •   ChssPly76    16 年前

    java.text.SimpleDateFormat toString()

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    dueDateStr = dateFormat.format(dueDate); // renders as 11/29/2009
    
        2
  •  1
  •   alphazero    16 年前

    Date dueDate = cal.getTime();
    String dueDateAsString = dueDate.toString();
    

    String dueDateAsFormattedString = DateFormat.format(dueDate);
    
        3
  •  0
  •   Spike Williams    16 年前

    您可能需要考虑使用 FastDateFormat 来自Apache commons,而不是SimpleDateFormat,因为SimpleDateFormat不是线程安全的。

    FastDateFormat dateFormat = FastDateFormat.getInstance("MM/dd/yyyy");
    dueDateStr = dateFormat.format(dueDate);
    

        4
  •  0
  •   Vojtěch    14 年前

    你可以用我的一个类轻松做到这一点: https://github.com/knyttl/Maite/wiki/Maite-Date-and-Time

    new Time()
        .plus(1, Time.DAY)
        .format("yyyy-MM-dd");