我希望能够在sql字符串的where子句中动态获取我想要使用的日期。
这是我的尝试。
GregorianCalendar c1= new GregorianCalendar();
GregorianCalendar c= new GregorianCalendar();
// get date a year ago
c1.add(Calendar.MONTH,-11);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("sdate", s);
Log.e("edate", e);
这会将开始日期显示为2010-02-18,结束日期为2014-03-18。
并且它不一致!...再次尝试,我得到了2008-10-18!
怎么能把它拉下来? 这是日期格式。
SimpleDateFormat fm =new SimpleDateFormat("yyyy-MM-dd");
我可以添加的是我正在使用微调器。
这是微调器选择的数组
String[] periods= {"This month","Two Months","Three Months","Six Months","Year","All"};
spinner = (Spinner) findViewById(R.id.payments);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,periods);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(this);
所以在on itemselected listener中我添加了。
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
if(periods[pos]=="This month")
{
// get data within one month
c1.add(Calendar.MONTH,-1);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("sdate", s);
Log.e("edate", e);
rentalpayments= new RentalPaymentsAdapter(this,s,e);
list.setAdapter(rentalpayments);
}else if(periods[pos]=="Two Months")
{
// get data within one month
c1.add(Calendar.MONTH,-1);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("sdate", s);
Log.e("edate", e);
rentalpayments= new RentalPaymentsAdapter(this,s,e);
list.setAdapter(rentalpayments);
}else if(periods[pos]=="Three Months")
{
// get data within one month
c1.add(Calendar.MONTH,-2);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("sdate", s);
Log.e("edate", e);
rentalpayments= new RentalPaymentsAdapter(this,s,e);
list.setAdapter(rentalpayments);
}
else if(periods[pos]=="Six Months")
{
// get data within one month
c1.add(Calendar.MONTH,-5);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("sdate", s);
Log.e("edate", e);
rentalpayments= new RentalPaymentsAdapter(this,s,e);
list.setAdapter(rentalpayments);
}
else if(periods[pos].equals("Year"))
{
// get data within one month
String test=periods[pos];
c1.add((GregorianCalendar.MONTH),-11);
Date myStartDate =c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
Log.e("test", test);
Log.e("sdate", s);
Log.e("edate", e);
rentalpayments= new RentalPaymentsAdapter(this,s,e);
list.setAdapter(rentalpayments);
}
RentalPaymentAdapter是一个基本适配器,它接受开始日期和enddate作为参数
rentalpayments= new RentalPaymentsAdapter(this,s,e);
任何线索?
答案 0 :(得分:0)
以下代码运行正常。正如上面的评论所述。看起来你的格式不合适。
DateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c = new GregorianCalendar();
// get date a year ago
c1.add(Calendar.MONTH, -11);
Date myStartDate = c1.getTime();
Date myendDate = c.getTime();
String s = fm.format(myStartDate);
String e = fm.format(myendDate);
System.out.println(String.format("sdate: %s", s));
System.out.println(String.format("edate: %s", e));
答案 1 :(得分:-1)
我认为您需要将代码更改为
c1.add((GregorianCalendar.MONTH), -11);
因为const可能不同,而方法api是
public void add(int field,int amount)
还使用修日期进行测试
//set date to last day of 2009
c1.set(Calendar.YEAR, 2009);
c1.set(Calendar.MONTH, 11); // 11 = december
c1.set(Calendar.DAY_OF_MONTH, 31);