Android中的格式日期佩戴表盘

时间:2017-12-20 10:58:37

标签: wear-os

我正在尝试使用此代码将Android Wear watchface中的日期格式化为“dd-MMM”

mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
mDateFormat.setCalendar(mCalendar);
mDateFormat.format("dd-MMM");

但是,我收到此错误,指向上面代码的最后一行。

  

12-20 04:32:37.896 5616-5616 / com.example.android.wearable.watchface   E / AndroidRuntime:致命异常:主要                                                                                         过程:com.example.android.wearable.watchface,PID:5616                                                                                         java.lang.IllegalArgumentException:无法将给定的Object格式化为   日期                                                                                             at java.text.DateFormat.format(DateFormat.java:302)                                                                                             at java.text.Format.format(Format.java:157)                                                                                             在   com.example.android.wearable.watchface.watchface.DigitalWatchFaceService $ Engine.initFormats(DigitalWatchFaceService.java:290)                                                                                             在   com.example.android.wearable.watchface.watchface.DigitalWatchFaceService $ Engine.onCreate(DigitalWatchFaceService.java:235)                                                                                             在   android.service.wallpaper.WallpaperService $ Engine.attach(WallpaperService.java:875)                                                                                             在   android.service.wallpaper.WallpaperService $ IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1166)                                                                                             在   com.android.internal.os.HandlerCaller $ MyHandler.handleMessage(HandlerCaller.java:37)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                             在android.os.Looper.loop(Looper.java:154)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:0)

我要说问题是java.text.DateFormat.format()没有将格式字符串作为参数 - 它需要Date个对象。要指定格式字符串,请使用android.text.format.DateFormat,如下所示:

mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);
String output = mDateFormat.format("dd-MMM", mCalendar);

或像这样使用java.text.SimpleDateFormat

mDateFormat = new SimpleDateFormat("dd-MMM");
String output = mDateFormat.format(mCalendar.getTime());

[代码在这里徒手打印,因此无法保证无错误!]

答案 1 :(得分:0)

你可以像这样简单地格式化

mDateFormat = new SimpleDateFormat(“dd-MMM”);         日期mDate;

这将从当前的上下文格式中获取:

mDateFormat = DateFormat.getDateFormat(DigitalWatchFaceService.this);

完整的代码是:

        java.text.DateFormat mDateFormat;
            mDateFormat = new SimpleDateFormat("dd-MMM");

        private void initFormats() {
            mDateFormat = new SimpleDateFormat("dd-MMM");
            mDateFormat.setCalendar(mCalendar);
        }

在某处画画:

                canvas.drawText(
                        mDateFormat.format(mDate),
                        mXOffset, mYOffset + mLineHeight * (float)1.5, mDatePaint);

您需要在onCreate上面调用:

        @Override
        public void onCreate(SurfaceHolder holder) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "onCreate");
            }
            super.onCreate(holder);


            Resources resources = DigitalWatchFaceService.this.getResources();

            mCalendar = Calendar.getInstance();
            mDate = new Date();
            initFormats();
        }