簡體   English   中英

如何將字符串格式化為 LocalDate

[英]how to format a string into a LocalDate

我有一個 android 應用程序,它需要用戶的出生日期,數據類型是 LocalDate,因為后端使用它來計算該用戶的年齡,並根據年齡分配他/她必須為注冊支付的費用。 我有一個按鈕,當點擊它時,它會彈出一個日歷對話框供用戶使用 select 出生日期並將按鈕上選擇的日期顯示為文本。按鈕上的文本是選擇的出生日期讀取並存儲為字符串,以便可以將其用作出生日期的參數。 該按鈕工作正常,但我在格式化時遇到問題,因為我想將字符串格式化為 LocalDate。 我收到這個錯誤,提示無法在索引 4 處解析文本“2000-10-23”。我不知道我做錯了什么。

Here is the button in the XML

<Button
                            android:id="@+id/btn_date_of_birth"
                            style="@style/Base.Widget.AppCompat.Spinner.Underlined"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="2"
                            android:text="select here"
                            android:theme="@style/EditText.Grey"
                            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                            android:textColor="@color/grey_40" />

Here is how I set the calendar and did the formatting

private void setDate(){

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        android.app.DatePickerDialog dialog = new DatePickerDialog(
                PatientRegistrationActivity.this,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                mDateSetListener,
                year,month,day);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;

                String date = year + "-" + month + "-" + day;
                btnDateOfBirth.setText(date);
            }
        };
    }
I now call the setDate method when the button is click

btnDateOfBirth.setOnClickListener(view -> {
            setDate();
        });

Now I want to format the date of birth displayed on the button and pass it to the date of birth which is of type LocalDate 

String dateTime = btnDateOfBirth.getText().toString();
userDto.setDateOfBirth(LocalDate.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")));

But this is giving me an error that says Text '2000-10-23' could not be parsed at index 4.
Please how can I resolve this issue, I really need help

這是將millieslong日期格式化為Date Format的簡單方法

fun getDate(milliSeconds: Long, dateFormat: String = "d MMM, yyyy"): String? {
// Create a DateFormatter object for displaying date in specified format.
val formatter = SimpleDateFormat(dateFormat)

// Create a calendar object that will convert the date and time value in milliseconds to date.
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = milliSeconds
return formatter.format(calendar.time)

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM