有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何以字符串格式“EEE,MMM d,yyyy”获取整数月、日和年

我希望你们能帮我。当我使用DatePicker时,我会以“EEE,MMM d,yyyy”格式显示我的日期

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());
            editTextStartDate.setText(strDate);

但在设置日期后,我将处理数据,我需要将数据格式化为“dd/mm/yyyy”,以便通过拆分来使用它:

    String startDate = editTextStartDate.getText().toString().trim();
    if (!startDate.isEmpty()) {
        String getTo[] = startDate.split("/");
        getToDateYear = Integer.parseInt(getTo[2]);
        getToDateMonth = Integer.parseInt(getTo[1]);
        getToDateDay = Integer.parseInt(getTo[0]);

        toTime.set(Calendar.YEAR, getToDateYear);
        toTime.set(Calendar.MONTH, getToDateMonth-1);
        toTime.set(Calendar.DAY_OF_MONTH, getToDateDay);
    }

对我来说最好的方法是什么?希望你们能帮助我教学和指导。谢谢你们


共 (1) 个答案

  1. # 1 楼答案

    String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());中不会有任何/

    因此,字符串getTo[]将不会有任何数组输出

    下面是将日期格式转换为另一种格式的解决方案,如下所示:

    SimpleDateFormat sourceDateFormat = new SimpleDateFormat("EEE, MMM d, yyyy");
    Date sourceDate = null;
    try {
        sourceDate = sourceDateFormat.parse(startDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyy");
    String targetdatevalue= targetFormat.format(sourceDate);
    

    因此,targetdatevalue将是您所需的输出