猿问

将谷歌日历日期转换为 java.util 以便我可以更改其格式?

我正在尝试更改我的 google 日历代码的输出日期格式,但它不能在 SimpleDateFormater 中使用,因为它是一个 google api 日期,而不是 java 版本。


我确实让它工作了,但最终删除了该代码,因为它总是返回 null。


我的应用程序活动将其显示为“(我的事件)在 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);


        } 

呼唤远方
浏览 114回答 2
2回答

慕的地8271018

获取自 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 项目。

肥皂起泡泡

Date&nbsp;javaDate&nbsp;=&nbsp;new&nbsp;Date(googleDate.getValue());然后您可以随意格式化它。
随时随地看视频慕课网APP

相关分类

Java
我要回答