为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2021-10-20 11:05:56
我正在尝试更改我的 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);        } 
查看完整描述

2 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

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


查看完整回答
反对 回复 2021-10-20
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

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

然后您可以随意格式化它。


查看完整回答
反对 回复 2021-10-20
  • 2 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信