如何在android中获取标准格式的日期

如何在android中获取标准格式的日期,android,sql,date,android-date,Android,Sql,Date,Android Date,如果我为eg尝试以下代码: 我将日期设置为2014年7月25日星期五上午10:00。以毫秒为单位的日期为1402080056000 现在,如果我尝试读取与下面相同的毫秒 long time = 1402080056000; Date mydate = new Date(time); mydate变量将日期显示为2014年6月25日星期六00:10:56 String DateTimeString = DateFormat.getDateTimeInstance().format

如果我为eg尝试以下代码:

我将日期设置为2014年7月25日星期五上午10:00。以毫秒为单位的日期为
1402080056000

现在,如果我尝试读取与下面相同的毫秒

  long time = 1402080056000;

  Date mydate = new Date(time); 
mydate变量将日期显示为2014年6月25日星期六00:10:56

  String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));
使用DateTimeString中的上述语句,我得到的日期为2014年6月25日上午12:10:56

如何读取2014年7月25日星期五上午10:00至1402080056000的日期时间请尝试以下代码:

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

只需要处理格式字符串

String dateTimeString= 
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));  
明确设置时区:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));  
此外,这将非常有用

尝试以下方法:

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

使用在Android上工作的Joda时间库,日期时间工作更容易

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 );

要将字符串解析为日期时间,请搜索StackOverflow中的“Joda parse”以查找许多示例。

您好,谢谢,如果我将时间戳传递为0,则得到的日期时间为01:01:70:05:30:00。但是为什么我得到的是早上5点30分,它应该是00对吗?如果您提供了错误的数据进行转换,那么默认情况下,它将转换为1970年1月1日这样的日期。因为时间戳计算从那个日期开始。所以你们必须给出有效的时间戳才能转换。嗨,我需要GMT格式的日期,但为什么我得到的是5.30 IST,@Naruto。。用这个。。您将获得GMT:)谢谢,但我得到了错误,DateFormat类型中的方法格式(日期)不适用于参数(字符串,日期):(@Naruto..现在试试…我已经编辑了…只需将您的毫秒数替换为日期,然后告诉mei尝试过,我仍然得到了错误的方法格式(日期)在类型中,DateFormat不适用于参数(字符串,日期)。如果我将0作为时间传递,我将获得日期为01-01-1970 05:30。为什么不给出GMT格式的时间。即,它应该给我01-01-1970 00:00