我在Android中创建了一个包含日期,月份和年份的Spinner
。我想将Spinner
的数据显示为格式为String
的{{1}}。但它总是包含错误:
dd/mm/yyyy
XML布局的一部分:
public static int compare(int lhs, int rhs) {
return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
}
private static NumberFormatException invalidInt(String s) {
throw new NumberFormatException("Invalid int: \"" + s + "\"");
}
代码
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/datePayment"
android:layout_width="75dp"
android:layout_height="44dp"
android:layout_marginRight="20dp"
android:background="#FFFFFF" />
<Spinner
android:id="@+id/monthPayment"
android:layout_width="100dp"
android:layout_height="46dp"
android:layout_marginRight="20dp"
android:background="#FFFFFF" />
<Spinner
android:id="@+id/yearPayment"
android:layout_width="60dp"
android:layout_height="48dp"
android:background="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp">
<Button
android:id="@+id/bsubmit"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:background="#6b6b6b"
android:text="Submit"
android:textColor="#ffffff" />
<Button
android:id="@+id/breset"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:background="#6b6b6b"
android:text="Reset"/>
</LinearLayout>
答案 0 :(得分:1)
下面的代码可以帮助您获得所需格式的日期。
final String[] month = {"Januari", "Februari", "Maret", "April", "Mei", "Juni",
"Juli", "Agustus", "September", "Oktober", "November", "Desember"};
final String[] date = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
final String[] year = {"2014", "2015"};
// This method identifies the month from the array
// Use this function instead of numMonth function in your original code.
// Its easier to maintain.
public int getMonth(String passedMonth) {
for (int i = 0; i < month.length; i++) {
// Returns the array index of the passedMonth from month Array
if (month[i].equals(passedMonth)) {
return (i+1);
}
}
return (-1); // Invalid month
}
// This method would get the input from your spinner and print the date as
// per your desired format.
public void printDateInMyFormat (String date, String month, String year)
{
// This is the format you are requesting for.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = date +"/"+ getMonth(month) +"/"+ year;
System.out.println("dateInString : "+dateInString);
try {
Date dt = formatter.parse(dateInString);
System.out.println("My Date ::"+dt);
System.out.println("My Required Date String :: " +formatter.format(dt));
} catch (ParseException e) {
e.printStackTrace();
}
}
<强>结果:强>
Input:
test.printDateInMyFormat ("01", "September", "2015");
<强>输出:强>
Month is : 9
dateInString : 01/9/2015
My Date :: Tue Sep 01 00:00:00 IST 2015
My Required Date String :: 01/09/2015
代码处理无效日期,如下所示:
Input:
test.printDateInMyFormat ("31", "September", "2015");
<强>输出:强>
Month is : 9
dateInString : 31/9/2015
My Date :: Thu Oct 01 00:00:00 IST 2015
My Required Date String :: 01/10/2015