将Google日历日期转换为java.util,以便更改其格式?

时间:2018-09-30 03:32:52

标签: java android calendar google-calendar-api

我正在尝试更改Google日历代码的输出日期格式,但由于它是Google api日期而不是Java版本,因此无法在SimpleDateFormater中使用。

我确实可以使用它,但是最终删除了该代码,因为它始终返回null。

我的应用活动将其显示为“(空时的我的事件)”,我的事件将在此更新,但日期始终返回null。

我不太擅长使用Googles API,并且对Java还是陌生的,因此将不胜感激!

这是我的代码:

import android.os.AsyncTask;

import com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * An asynchronous task that handles the Google Calendar API call.
 * Placing the API calls in their own task ensures the UI stays responsive.
 */

/**
 * Created by miguel on 5/29/15.
 */

public class ApiAsyncTask3 extends AsyncTask<Void, Void, Void> {
    private MainActivity mActivity3;

    /**
     * Constructor.
     * @param activity MainActivity that spawned this task.
     */
    ApiAsyncTask3(MainActivity activity) {
        this.mActivity3 = activity;
    }

    /**
     * Background task to call Google Calendar API.
     * @param params no parameters needed for this task.
     */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            mActivity3.clearResultsText();
            mActivity3.updateResultsText(getDataFromApi());

        } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
            mActivity3.showGooglePlayServicesAvailabilityErrorDialog(
                    availabilityException.getConnectionStatusCode());

        } catch (UserRecoverableAuthIOException userRecoverableException) {
            mActivity3.startActivityForResult(
                    userRecoverableException.getIntent(),
                    BlockOrderGCalDaily.REQUEST_AUTHORIZATION);

        } catch (IOException e) {
            mActivity3.updateStatus("The following error occurred: " +
                    e.getMessage());
        }
        return null;
    }

    /**
     * Fetch a list of the next 10 events from the primary calendar.
     * @return List of Strings describing returned events.
     * @throws IOException
     */
    private List<String> getDataFromApi() throws IOException {
        // List the next 10 events from the primary calendar.
        DateTime now = new DateTime(System.currentTimeMillis());
        List<String> eventStrings = new ArrayList<String>();
        Events events = mActivity3.mService.events().list("9sfoekroead5j1sb8aduqgvog4@group.calendar.google.com")
                .setMaxResults(1)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();
        List<Event> items = events.getItems();

        for (Event event : items) {
            DateTime start = event.getStart().getDateTime();
            if (start == null) {
                // All-day events don't have start times, so just use
                // the start date.
                start = event.getStart().getDate();

            }



            eventStrings.add(
                    String.format("%s on %s", event.getSummary(), start));
        }
        return eventStrings;
    }

}

2 个答案:

答案 0 :(得分:0)

获取自1970-01-01T00:00:00Z的纪元参考以来的毫秒数。致电com.google.api.client.util.DateTime::getValue

long millis = myDateTime.getValue() ;

将它们解析为java.time.Instant

Instant instant = Instant.ofEpochMilli( millis ) ;

从UTC调整为您关心的时区。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

生成一个字符串。

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = zdt.format( f ) ;

对于早期的Android,请参见ThreeTenABP项目。

答案 1 :(得分:0)

Date javaDate = new Date(googleDate.getValue());

然后您可以选择格式化它。