if i try the below code for eg:

i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,

now if i try to read the same milliseconds to date as below

  long time = 1402080056000;

  Date mydate = new Date(time); 

mydate variable shows date as Sat Jun 25 00:10:56 IST 2014

  String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));

with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM

How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM

有帮助吗?

解决方案

Just need to work on the format string,

String dateTimeString= 
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));  

Explicitly set time zone:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));  

Also, this would be useful Regarding Timezones and Java

其他提示

try below code:

private String convertMilliToDate(long timestamp) {

        DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");

        // Create a calendar object that will convert the date and time value in
        // milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }

Try this:

String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();

Date-time work is easier using the Joda-Time library, which works on Android.

long millis = 1402080056000L;

DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );

DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );

DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );

To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top