Android SimpleDateFormat和未来日期

时间:2011-12-21 09:29:26

标签: android date

long time = 1342580400; //This should be GMT: Wed, 18 Jul 2012 03:00:00 GMT
Date date = new Date(time);
SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM");
String strTime = dateSdf.format(date);
System.out.println(strTime); //This gives me 16 Jan instead.

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

 Date dt = new Date();

            // Fri Dec 02 17:23:13 GMT+05:30 2011

            DateFormat gmtFormat = new SimpleDateFormat();
            TimeZone gmtTime = TimeZone.getTimeZone("GMT");
            gmtFormat.setTimeZone(gmtTime);
            gmtFormat.format(dt);
            Calendar new_c = gmtFormat.getCalendar();

            int hours = new_c.get(Calendar.HOUR_OF_DAY);
            int minutes = new_c.get(Calendar.MINUTE);
            int seconds = new_c.get(Calendar.SECOND);

            mYear = new_c.get(Calendar.YEAR);
            MMonth = new_c.get(Calendar.MONTH);
            mDay = new_c.get(Calendar.DAY_OF_MONTH);
            new_c.setTimeZone(tz);

            MMonth = MMonth + 1;

            String curdate = mDay + "-" + MMonth + "-" + mYear;

答案 1 :(得分:1)

如果您运行以下代码:

  SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss Z");
  Date d = dateSdf.parse("18 Jul 2012 03:00:00 GMT");
  System.out.println(d.getTime());

您将获得输出:1342580400000

所以你的问题是你缺少一些数字。

答案 2 :(得分:1)

似乎您从时间手动计算日期是错误的。我尝试了以下代码:

long currTime = System.currentTimeMillis();

    Date date = new Date(currTime);
    SimpleDateFormat dateSdf = new SimpleDateFormat("dd MMM");
    String strTime = dateSdf.format(date);
    System.out.println(strTime); 

它给了我正确的日期,即12月21日