繁体   English   中英

在我的代码中设置日期方面需要帮助

[英]Need help on setting dates in my code

我希望能够动态获取要在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侦听器中,我添加了。

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是一个基本适配器,它接受开始日期和结束日期作为参数

rentalpayments= new RentalPaymentsAdapter(this,s,e);

有什么线索吗?

下面的代码工作正常。 如以上评论中所述。 看来您的格式不正确。

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));

我认为您需要将代码更改为

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);

暂无
暂无

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

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