简体   繁体   English

适用于Azure移动服务的Android客户端中的日期功能

[英]Date Functions in Android Client for Azure Mobile Service

I have an Azure Mobile Service which returns date to an Android Client in this format "Sat Sep 27 22:48:48 PDT 2014" 我有一个Azure移动服务,它以“ Sat Sep 27 22:48:48 PDT 2014”格式向Android客户端返回日期

I want to calculate the difference between this returned date and today's date. 我想计算此返回日期与今天的日期之间的差。 After much iterations here is my current function. 经过多次迭代后,这是我当前的功能。

 public String calculateDayDifference(String DateFromAzure){

        SimpleDateFormat AzureDateFormat = new SimpleDateFormat("EEE MMM dd H:m:s yyyy");
        Calendar cal = Calendar.getInstance(Locale.getDefault());
        String result;

        try {
           String currentDate = AzureDateFormat.format(System.currentTimeMillis() * 1000L);
           Date presentDate = AzureDateFormat.parse(currentDate);
           Date billDueDate = AzureDateFormat.parse(DateFromAzure);
           long diff = billDueDate.getTime() - presentDate.getTime(); 
           result = Long.toString(diff);
        } catch (ParseException e) {
            result = Long.toString(- 1);
            //e.printStackTrace();
        }
        return  result;
    }

And here is how I call this function from my Adapter's getView() method 这是我从适配器的getView()方法调用此函数的方式

 viewHolder.txtNumberDays.setText(mDateFunctions.calculateDayDifference(
                viewHolder.billSummary.getBillDueDate().toString()))

And here is the Java class field that maps to the date column in the Mobile Service table. 这是Java类字段,它映射到Mobile Service表中的date列。

  @SerializedName("billDueDate")
    private Date BillDueDate;

No matter how I tweak it, it give me negative result like so. 无论我如何调整,它都会给我带来负面结果。 How can I re-write the method above to return the difference between today's date and the date returned from Azure Mobile Service table? 如何重写上面的方法以返回今天的日期和从Azure移动服务表返回的日期之间的差?

Azure移动服务Android显示日期

Ok, from the AMS Android SDK source code I can see that the SDK handles formatting and parsing of dates automatically so what is returned is a valid java.util.date object. 好的,从AMS Android SDK源代码中,我可以看到SDK自动处理日期的格式和解析,因此返回的是有效的java.util.date对象。

https://github.com/Azure/azure-mobile-services/blob/master/sdk/android/src/sdk/src/com/microsoft/windowsazure/mobileservices/DateSerializer.java https://github.com/Azure/azure-mobile-services/blob/master/sdk/android/src/sdk/src/com/microsoft/windowsazure/mobileservices/DateSerializer.java

With this realization I just called getTime() method on the returned date so I can compare it again current date which can be obtained from System.CurrentTimeMillis(). 通过这种实现,我只是在返回的日期上调用了getTime()方法,因此可以再次比较它的当前日期,该日期可以从System.CurrentTimeMillis()获得。 I then converted the result to int and that was with. 然后,我将结果转换为int,结果是这样。 Long learning experience. 长期的学习经验。 Its not the most efficient way to handle this but it works for. 它不是处理此问题的最有效方法,但它确实适用。 Here is the code 这是代码

  mBillDueDateService.mBillTable
                .execute(new TableQueryCallback<Bill>() {
            @Override
            public void onCompleted(List<Bill> result, int count,
                                    Exception exception, ServiceFilterResponse response) {
                if (exception == null){
                   for (Bill bill : result){
                       long timeInLong = bill.getBillDueDate().getTime();
                       long currentTime = System.currentTimeMillis();
                       long diffTime = timeInLong - currentTime;
                       long diffDays = diffTime / (1000 * 60 * 60 * 24);
                       int daysdiff = (int) diffDays;
                       Log.i(TAG, "Bill Name " + bill.getBillName() + "" + "Due in " + daysdiff + "days");
                       bill.setDaysBeforeDueDate(daysdiff);
                        mUpcomingBillAdapter.add(bill);
                    }

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

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