繁体   English   中英

在日历选择器视图中以DD / MM / YYY格式设置日期以选择多个日期(Android)

[英]Format dates in DD/MM/YYY for multiple date selections in calendar picker view(Android)

我有一个日历选择器,可以在其中选择多个日期。下面有一个按钮可以显示我选择的日期。但是我得到的日期格式为[MON OCT 12 00:00:00 GMT + 530,FRI OCT 16 00:00:00 GMT + 05:30 2015,SAT OCT 17 00:00:00 GMT + 05:30 2015] 。但我的要求是以以下格式获取日期12/10/2015,16/10 / 2015年,/ 17/10/2015。如何实现相同目标? 提前致谢。 下面是我使用的代码

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View rootView2 = inflater.inflate(R.layout.monthly_planner, container, false);
    Calendar nextYear=Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 1);
    calendar=(CalendarPickerView) rootView2.findViewById(R.id.calendar_view);
    Date today=new Date();
    calendar.init(today,nextYear.getTime()).withSelectedDate(today).inMode(CalendarPickerView.SelectionMode.MULTIPLE);
   // calendar.highlightDates(getHolidays());
    dropdown=(Spinner) rootView2.findViewById(R.id.monthly_planner_spinner);
    String[] items1 = new String[]{"Select Something"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, items1);
    dropdown.setAdapter(adapter);
    button= (Button) rootView2.findViewById(R.id.action_next);
    Button button1=(Button) rootView2.findViewById(R.id.btnconfirm);
    datesSelected = (TextView) rootView2.findViewById(R.id.dateView);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), Order_ConfirmationActivity.class));
        }
    });
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ArrayList<Date>selectedDates=(ArrayList<Date>)calendar
                    .getSelectedDates();
//                Toast.makeText(getActivity(), selectedDates.toString(),
//                        Toast.LENGTH_LONG).show();
            String dates=selectedDates.toString();
            SimpleDateFormat df=new SimpleDateFormat("dd/mm/yyyy");
            for (int i=0;i<dates.length();i++)
            {
                 date[i] = df.format(dates);
            }
          for (int i=0;i<date.length;i++){
              datesSelected.setText(date[i]);
          }
        }
    });
    return rootView2;
}

您需要对Date对象而不是String对象应用格式,如下所示:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = Calendar.getInstance().getTime();
System.out.println(sdf.format(date)); // 12/10/2015

因此,在您的情况下,您在selectedDates中具有日期的数组列表,则需要在此arraylist中格式化日期对象。 并请注意格式:dd / MM / yyyy(大写M)。 有关更多格式,请检查SimpleDateFormat javadoc中的日期和时间模式。

[更新]我猜您正在使用CalendarPickerView允许选择多个日期,您可以通过calendar.getSelectedDates()进行检索,然后尝试在datesSelected (TextView)中显示多个日期。 您可以尝试以下方法:

ArrayList<Date>selectedDates = (ArrayList<Date>)calendar.getSelectedDates();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
for(int i = 0; i< selectedDates.size(); i++)
{
   Date tempDate = selectedDates.get(i);
   String formattedDate = sdf.format(tempDate);
   datesSelected.append(formattedDate);
   //Following if is added to avoid adding comma after the last date.
   if(i != selectedDates.size() -1)
   {
     datesSelected.append(",");
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM