2017-12-20 14 views
0

이 코드 그러나형식 날짜

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

를 사용하여 "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가 : java.text.Format.format (Format.java:157)에서 java.text.DateFormat.format (DateFormat.java:302) 에서 날짜 로 지정된 객체를 포맷 할 수 없습니다 (디지털 시계 온도계) ce.java:290) android.service.wallpaper.WallpaperService $ Engine.attach에서 com.example.android.wearable.watchface.watchface.DigitalWatchFaceService $ Engine.onCreate (DigitalWatchFaceService.java:235) (WallpaperService에서 . 자바 : 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 (기본 메소드) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit. 자바 : com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)

답변

0

에서 886) 나는 문제가 java.text.DateFormat.format()가 매개 변수로 형식 문자열을하지 않는다는 것입니다라고 말하고 싶지만 - Date 개체가 필요합니다. 이 같은 java.text.SimpleDateFormat

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

또는 사용 : 형식 문자열을 지정 하나를 사용 같은 android.text.format.DateFormat을하려면

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

[코드가 너무 버그 무료 보장 할 수 없습니다, 여기에 자유에 입력했다!]

0

당신은 단순히이

mDateFormat = 새로운하여 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); 

을 당신은 위에서 호출 할 필요가 귀하의 생성 :

 @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(); 
     }