我在 Android 中创建了一个包含日期、月份和年份的 Spinner
。我想将 Spinner
中的数据显示到 String
中,格式为 dd/mm/yyyy
。但它总是包含错误:
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 + "\"");
}
我的部分 XML 布局:
<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>
代码
public class Payment extends Activity implements OnItemSelectedListener
{
private Spinner spDate, spMonth, spYear;
private static 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"};
private static final String[] month = {"Januari", "Februari", "Maret", "April",
"Mei", "Juni", "Juli", "Agustus", "September",
"Oktober", "November", "Desember"};
private static final String[] year = {"2014", "2015"};
private Button bSubmit;
String tampspDate, tampspMonth, tampspYear, numberMonth, tampNumberMonth;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.payment);
spDate = (Spinner)findViewById(R.id.datePayment);
spMonth = (Spinner)findViewById(R.id.monthPayment);
spYear = (Spinner)findViewById(R.id.yearPayment);
ArrayAdapter<String> AdaptDate = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, date);
AdaptDate.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spDate.setAdapter(AdaptDate);
ArrayAdapter<String> AdaptMonth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, month);
AdaptMonth.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMonth.setAdapter(AdaptMonth);
ArrayAdapter<String> AdaptYear = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, year);
AdaptYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spYear.setAdapter(AdaptYear);
bSubmit = (Button)findViewById(R.id.bsubmit);
spDealer.setOnItemSelectedListener(this);
spDate.setOnItemSelectedListener(this);
spMonth.setOnItemSelectedListener(this);
spYear.setOnItemSelectedListener(this);
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tampTanggal = ""+tampspDate+ " "+tampNumberMonth+" "+tampspYear;
Toast.makeText(Payment.this, tampTanggal, Toast.LENGTH_SHORT).show();
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tampspDate = spDate.getSelectedItem().toString();
tampspMonth = spMonth.getSelectedItem().toString();
tampspYear = spYear.getSelectedItem().toString();
tampNumberMonth = numMonth(tampspMonth);
}
public String numMonth(String abc){
if(abc.equals("Januari")){
numberMonth="01";
}
else if(abc.equals("Februari")){
numberMonth="02";
}
else if(abc.equals("Maret")){
numberMonth="03";
}
else if(abc.equals("April")){
numberMonth="04";
}
else if(abc.equals("Mei")){
numberMonth="05";
}
else if(abc.equals("Juni")){
numberMonth="06";
}
else if(abc.equals("Juli")){
numberMonth="07";
}
else if(abc.equals("Agustus")){
numberMonth="08";
}
else if(abc.equals("September")){
numberMonth="09";
}
else if(abc.equals("Oktober")){
numberMonth="10";
}
else if(abc.equals("November")){
numberMonth="11";
}
else if(abc.equals("Desember")){
numberMonth="12";
}
return numberMonth;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
最佳答案
下面的代码可以帮助您获取所需格式的日期。
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
关于android - 将 Spinner 转换为字符串中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25497569/