【问题标题】:How to format a date android?如何格式化日期android?
【发布时间】:2013-07-16 17:21:21
【问题描述】:

我在字符串中有一个日期,我使用这个:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
dataUltima = sdf.format(sdf);

格式化日期。但此方法返回系统的实际日期。如何只格式化日期而不更改日期。

观察。我从服务器上得到这个日期,它来找我 yyy-dd-MM 我想要它dd/MM/yyyy

【问题讨论】:

  • 您从服务器获取的日期类型是什么?
  • 我正在从服务器获取一个字符串。

标签: android android-date


【解决方案1】:

我不确定你的问题是什么。

试试看。也许有帮助。

String server_format = "2013-01-02";    //server comes format ?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {

    Date date = sdf.parse(server_format);
    System.out.println(date);
    String your_format = new SimpleDateFormat("dd/MM/yyyy").format(date);
    System.out.println(your_format);    //you want format ?

} catch (ParseException e) {
    System.out.println(e.toString()); //date format error
}

【讨论】:

    【解决方案2】:

    请关注:

    try
    {
      //create SimpleDateFormat object with source string date format
      SimpleDateFormat sdfSource = new SimpleDateFormat("yyy-dd-MM");
    
      //parse the original date string(strDate) that you are getting from server into Date object
      Date date = sdfSource.parse(strDate);
    
      //create SimpleDateFormat object with desired date format
      SimpleDateFormat sdfDestination = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    
      //parse the date into another format
      strDate = sdfDestination.format(date);
    
      System.out.println("Date is converted");
      System.out.println("Converted date is : " + strDate);
    
    }
    catch(ParseException pe)
    {
      System.out.println("Parse Exception : " + pe);
    }
    

    来源:Convert date string from one format to another format using SimpleDateFormat

    希望这会有所帮助。

    【讨论】:

    • 我收到一个错误,nullpointexception 07-16 14:41:10.709: E/AndroidRuntime(28291): Caused by: java.lang.NullPointerException 07-16 14:41:10.709: E /AndroidRuntime(28291): 在 java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001) 07-16 14:41:10.709: E/AndroidRuntime(28291): 在 java.text.DateFormat.parse(DateFormat.java: 624) 07-16 14:41:10.709: E/AndroidRuntime(28291): 在 com.example.Tappa.SegundaTelaActivity.onCreate(SegundaTelaActivity.java:171)
    • 您的活动中的171 是哪一行?还要确保strDate 不为空,然后再将其传递给它以转换日期。您是否记录了来自您的服务器的日期。它是非空的吗?
    【解决方案3】:

    试试这个代码:

        Date newDate = new Date();
    
        // Print the date with the server format
        SimpleDateFormat sdfServer = new SimpleDateFormat("yyy-dd-MM ");
        System.out.println("Server date " + sdfServer.format(newDate));
    
        // Create a new formatter to get the date in the format you want
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String dataUltima = sdf.format(newDate);
        System.out.println("System date " + dataUltima);
    

    【讨论】:

      【解决方案4】:

      试试这个:

      String date = "2011-11-12 11:11:11"; //Fill with sample date received from server
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
      Date testDate = null;
      try {
          testDate = sdf.parse(date);
      }catch(Exception ex){
          ex.printStackTrace();
      }
      SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
      String dataUltima = formatter.format(testDate);
      System.out.println("Date is "+dataUltima );
      

      【讨论】:

      • 我收到一个错误原因:java.lang.IllegalArgumentException 07-16 14:50:00.239: E/AndroidRuntime(29237): at java.text.DateFormat.format(DateFormat. java:365) 07-16 14:50:00.239: E/AndroidRuntime(29237): at java.text.Format.format(Format.java:93)
      • 你的服务器日期是什么时候?如果您从服务器获取的日期的格式为“yyy-dd-MM”,仅带有任何时间组件,这对我有用。这是怎么来的?否则会报错
      • 它以字符串形式出现:“yyyy-dd-MM HH:mm:ss”,我想要它 dd/MM/yyy HH:mm
      • 哦,在你的问题中,你说的只是 yyy-dd-mm。然后你必须按原样格式化它。已编辑
      猜你喜欢
      • 1970-01-01
      • 2014-01-19
      • 2011-10-06
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多