【问题标题】:Unable to get date in the appropriate format in android无法在android中以适当的格式获取日期
【发布时间】:2014-09-15 14:56:30
【问题描述】:

我正在获取Date 并转换为GMT 格式。我以Thu Jul 24 06:55:56 GMT+05:30 2014 的形式获取它。我希望日期以6/19/2014 12:28:44 PM 的形式显示。谁能一步一步告诉我怎么做。我阅读了以下文档http://developer.android.com/reference/java/text/SimpleDateFormat.html,但即使我使用aL,格式也往往保持不变。我在下面发布代码,请指导我。

 SimpleDateFormat dateFormatGmt = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
 dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
 SimpleDateFormat dateFormatLocal = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
 //Time in GMT
     try {
        dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
        Log.i("gmt time",""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
        date_edittext.setText(""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

    标签: android simpledateformat gmt


    【解决方案1】:

    使用这个:

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
    

    说它是否有效:)

    【讨论】:

      【解决方案2】:
      SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm a");
      String date = format.format(Date.parse("Your date string"));
      
      
      String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
      SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
      String currentDate = format.format(strCurrentDate);
      
      format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
      String date = format.format(Date.parse(currentDate));
      

      【讨论】:

        【解决方案3】:

        我不太明白你的问题,但请看:

        Date test = new Date();
        test.setHours(10);
        SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy HH:mm:ss a");
        System.out.println(sdf.format(test));
        

        这显示 2014 年 7 月 24 日上午 10:13:01
        如果我将 setHours 更改为 test.setHours(22) 这将显示 7/24/2014 10:13:01 PM
        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-07
          • 2011-12-06
          • 2017-11-18
          • 2015-04-04
          相关资源
          最近更新 更多