-2

I would like to convert json timestamp with time zone to string that's presented on an android device.

Json timestamp is: /Date(1379488920000+0200)/

Output should consider time zone that android device is in and format (AM/PM or 24 hour) that is configured on device. Output should only show hours and minutes.

This code works, but it's not taking care of time zone, device time zone and device format:

String timestamp = jsonDate.split("\\(")[1].split("\\+")[0];
String departure = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT)
                        .format(new Date(Long.parseLong(timestamp)));
Goran
  • 19
  • 7

1 Answers1

0

I ended up setting context inside my constructor, and passing it on from overridden run function.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new FetchData(context, appWidgetManager), 1, 30000);
}

private class FetchData extends TimerTask {
    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    Context thisContext = null;

    public FetchData(Context context, AppWidgetManager appWidgetManager) 
    {
        thisContext = context;
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
    }

    @Override
    public void run() 
    {
        String restUrl = TrainFromSki;
        remoteViews.setTextViewText(R.id.widget_textview, GetTextData(thisContext, restUrl));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }
}

Inside GetTextData, I do following to get date formatted according to device setting:

String jsonDate = "/ Date(1379488920000+0200) /";
String timestamp = jsonDate.split("\\(")[1].split("\\+")[0];

String departure = DateFormat.getTimeFormat(context)
           .format(new Date(Long.parseLong(timestamp)));
Goran
  • 19
  • 7