如何以2017年8月1日的格式获取日期?

时间:2017-08-22 06:58:18

标签: android

我有一个应用程序,其中我必须从当前日期获得最后七天工作正常,但我得到dd-MM-yyyy格式的日期但我想在2017年8月1日得到它。我该怎么办请告诉我。

 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();
    Log.e(TAG,"Current date:::"+dateFormat.format(date));
    String currentDate = dateFormat.format(date);


    DateFormat dateFormat1 = new SimpleDateFormat("dd-MM-yyyy");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, -7);
    String sevenDaysBefore = dateFormat1.format(cal.getTime());
    Log.e(TAG,"seven days before::"+sevenDaysBefore);

3 个答案:

答案 0 :(得分:1)

将Formater更改为("dd-MMM-yyyy")

DateFormat dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -7);
String sevenDaysBefore = dateFormat1.format(cal.getTime());
Log.e(TAG,"seven days before::"+sevenDaysBefore);

答案 1 :(得分:0)

使用此

DateFormat dateFormat1 = new SimpleDateFormat("dd MMM yyyy");

答案 2 :(得分:0)

private void dateformation() {
    SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
    Calendar currentDate = Calendar.getInstance();

    String strDate = dateformate.format(currentDate.getTime());
    System.out.println("Date is :  " + strDate);


    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy");

    sdf.applyPattern("dd MMM, yyyy");
    Date x = new Date(currentDate.get(Calendar.YEAR) - 1900,
            currentDate.get(Calendar.MONTH),
            currentDate.get(Calendar.DAY_OF_MONTH));
    System.out.println(sdf.format(x));

}