【问题标题】:Convert string to Date in java在java中将字符串转换为日期
【发布时间】:2012-04-14 06:17:55
【问题描述】:

我正在尝试将字符串解析为 android 应用程序中的日期字段,但我似乎无法正确理解。这是我要转换为日期“03/26/2012 11:49:00 AM”的字符串。我正在使用的功能是:

private Date ConvertToDate(String dateString){
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return convertedDate;
}

但结果我不断收到3/1/112 11:49AM

【问题讨论】:

  • 您在哪里看到“3/1/112 11:49AM”?返回的值是一个日期,而不是一个字符串,所以你必须做 something 才能看到它作为一个字符串结果...
  • 我看到 Mon Mar 26 11:49:00 IST 2012 作为输出。
  • 尝试使用 dateFormat.setLenient(true) 将解析设置为宽松,然后检查解析结果
  • 可能您的 avd 配置不正确。有时它会发生在我身上。
  • 顺便使用 jodatime 来避免这种极端情况stackoverflow.com/questions/6841333/…

标签: java android date


【解决方案1】:

我猜你显示数据的方式是错误的,因为对我来说:

    String dateString = "03/26/2012 11:49:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(convertedDate);

打印:

Mon Mar 26 11:49:00 EEST 2012

【讨论】:

  • 每个人都是对的。它转换正确,但我使用的是 Date.getYear()、getMonth() 和所有我使用不正确的那些。感谢您的所有帮助。
【解决方案2】:

当我在SimpleDateFormat 中使用Locale.US 参数时一切正常

String dateString = "15 May 2013 17:38:34 +0300";
System.out.println(dateString);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String formattedDate = null;
Date convertedDate = new Date();
try {
     convertedDate = dateFormat.parse(dateString);
System.out.println(dateString);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 System.out.println(convertedDate);

【讨论】:

    【解决方案3】:
    String str_date="13-09-2011";
    DateFormat formatter ; 
    Date date ; 
    formatter = new SimpleDateFormat("dd-MM-yyyy");
    date = (Date)formatter.parse(str_date); 
    System.out.println("Today is " +date.getTime());
    

    试试这个

    【讨论】:

      【解决方案4】:

      此代码将帮助您生成类似 FEB 17 20:49 的结果。

          String myTimestamp="2014/02/17 20:49";
      
          SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
          Date date = null;
          Date time = null;
          try 
          {
              date = form.parse(myTimestamp);
              time = new Date(myTimestamp);
              SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
              SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
              String newDateStr = postFormater.format(date).toUpperCase();
              String newTimeStr = sdf.format(time);
              System.out.println("Date  : "+newDateStr);
              System.out.println("Time  : "+newTimeStr);
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      

      结果:

      日期:2 月 17 日

      时间:20:49

      【讨论】:

        【解决方案5】:
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString = "07/06/2013";
        
        try {
        
            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));
        
        } catch (ParseException e) {
            e.printStackTrace();
        }
        

        输出:

        2014/08/06 16:06:54
        2014/08/06 16:06:54
        

        【讨论】:

          【解决方案6】:
          GregorianCalendar date;
          
          CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date);
          
          Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();
          

          【讨论】:

            猜你喜欢
            • 2016-09-07
            • 1970-01-01
            • 1970-01-01
            • 2011-06-13
            • 2012-02-17
            • 2012-07-02
            • 1970-01-01
            相关资源
            最近更新 更多