Bob Bob - 5 months ago 26
Android Question

Wrong datetime in android

I need to get datetime 2015-11-14 11:20

Log.d(myLog,new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Calendar.getInstance().getTime()));


But I get 2015-11-13 18:52

Answer

Try this Code:

        Calendar c = Calendar.getInstance();
        System.out.println("Current time => "+c.getTime());

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        // formattedDate have current date/time
        Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();