gpt4 book ai didi

android - 如何将日期添加到当前日期?

转载 作者:行者123 更新时间:2023-11-30 01:44:21 25 4
gpt4 key购买 nike

我想在当前日期中加上 90 天,这是我的代码。我尝试了在线示例,但它似乎不起作用。

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Calendar date = Calendar.getInstance();
couponcreated = dateFormat.format(date);

date.add(Calendar.DATE,90);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MM-yyyy");
couponexpires = dateFormat1.format(date.getTime());

couponexpiredate.setText(couponexpires);

但是,我遇到错误并导致我的应用程序崩溃。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.loginandregistration/info.androidhive.loginandregistration.CouponReceivePageActivity}: java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
at java.text.DateFormat.format(DateFormat.java:296)
at java.text.Format.format(Format.java:93)
at info.androidhive.loginandregistration.CouponReceivePageActivity.onCreate(CouponReceivePageActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)

at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5419)
  at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)

这是我的 logcat 代码。

最佳答案

你的错误是当你第一次尝试格式化日期时
你应该写:

couponcreated = dateFormat.format(date.getTime());

代替

couponcreated = dateFormat.format(date);

因为日历不能直接格式化,你需要使用getTime()函数从日历中获取日期

关于android - 如何将日期添加到当前日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com