【问题标题】:How do I change date time format in Android?如何在 Android 中更改日期时间格式?
【发布时间】:2013-06-25 06:51:33
【问题描述】:

我在 Android 中使用以下格式显示日期和时间:
2013-06-18 12:41:24

如何将其更改为以下格式?
18-jun-2013 12:41 pm

【问题讨论】:

  • 你可以发布你的示例代码来获取日期时间吗?
  • MMM-dd-yyyy hh:mm a 怎么样?你自己试过吗?
  • 我在 sqllite 数据库中获取日期和时间
  • @AnilMH 发布你从 sqllite 获取日期时间代码!
  • lv_formatter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); lv_formatter.setTimeZone(TimeZone.getDefault());

标签: android


【解决方案1】:

这是工作代码

public String parseDateToddMMyyyy(String time) {
    String inputPattern = "yyyy-MM-dd HH:mm:ss";
    String outputPattern = "dd-MMM-yyyy h:mm a";
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}

文档:SimpleDateFormat | Android Developers

【讨论】:

    【解决方案2】:
    public class MainActivity extends AppCompatActivity {
        private Date oneWayTripDate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String date ="2017-05-05 13:58:50 ";
            SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy @hh:mm:ss aa");
            try {
                oneWayTripDate = input.parse(date);  // parse input
            } catch (ParseException e) {
                e.printStackTrace();
           }
           Log.e("===============","======currentData======"+output.format(oneWayTripDate);
        }
    }
    

    【讨论】:

    • 这看起来根本不像提供所需的日期格式。
    【解决方案3】:

    我在我的项目中修改了 Rusabh 的通用日期格式的答案

        public String formatDate(String dateToFormat, String inputFormat, String outputFormat) {
            try {
                Logger.e("DATE", "Input Date Date is " + dateToFormat);
    
                String convertedDate = new SimpleDateFormat(outputFormat)
                        .format(new SimpleDateFormat(inputFormat)
                                .parse(dateToFormat));
    
                Logger.e("DATE", "Output Date is " + convertedDate);
    
                //Update Date
                return convertedDate;
    
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
    
        }
    

    用法:

                String inputFormat = "yyyy-MM-dd'T'HH:mmz";
                String OutPutFormat = "MMMM dd', 'yyyy hh:mma";
                String convertedDate = formatDate(asOfDateTime, inputFormat, OutPutFormat);
    

    【讨论】:

    • 请不要教年轻人毫无保留地使用SimpleDateFormat作为第一选择。我知道这是内置在不是很新的 Android 上的东西,但它也是一个过时且臭名昭著的麻烦类。今天我们在java.time, the modern Java date and time API, 和它的DateTimeFormatter 中有很多更好的东西。是的,当您将ThreeTenABP 添加到您的 Android 编程项目时,您也可以在较低 API 级别的 Android 上使用。
    【解决方案4】:

    设置这样的答案来设置日期格式

     Date date=new Date("Your date "); 
    SimpleDateFormat formatter5=new SimpleDateFormat("required format");
    

    【讨论】:

      【解决方案5】:

      你必须这样设置

       Date date=new Date("Your date "); 
      SimpleDateFormat formatter5=new SimpleDateFormat("required format");
      String formats1 = formatter5.format(date);
      System.out.println(formats1);
      

      【讨论】:

        【解决方案6】:

        使用如下代码:

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
        String date = formatter.format(Date.parse("Your date string"));
        

        希望对你有所帮助。

        【讨论】:

        • 我在 sql 中获取日期和时间,我可以传递给“你的日期字符串”
        【解决方案7】:
            SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy  hh:mm a");
            String date = format.format(Date.parse("Your date string"));
        

        【讨论】:

        • 我在 sql 中获取日期和时间,我可以传递给“你的日期字符串”
        • 即使你在Sql中检索日期,你也有一个对象Date。所以可以使用 toString() 方法: String date = format.format(Date.parse(sqlDate.toString()));
        【解决方案8】:

        使用下面的代码:

        Date date=new Date("2013-06-18 12:41:24"); 
        SimpleDateFormat formatter5=new SimpleDateFormat("dd-MM-yyyy hh:mm a");
        String formats1 = formatter5.format(date);
        System.out.println(formats1);
        

        【讨论】:

          【解决方案9】:

          首先使用您的Date 对象创建一个Calendar 对象。然后使用您需要的日期、年份、月份等构建String。然后就可以使用了。

          您可以使用Calendar 类中的get() 方法获取数据。

          【讨论】:

            【解决方案10】:

            我们可以使用这种格式来转换日期。将日期作为参数传递

              public String formatdate(String fdate)
            {
                String datetime=null;
                DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
                SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date convertedDate = inputFormat.parse(fdate);
                    datetime = d.format(convertedDate);
            
                }catch (ParseException e)
                {
            
                }
                return  datetime;
            
            
            }
            

            【讨论】:

              【解决方案11】:
              private val dateFormatter: Format = SimpleDateFormat(
                  android.text.format.DateFormat.getBestDateTimePattern(
                      Locale.getDefault(),
                      "dMMyyjjmmss"
                  ),
                  Locale.getDefault()
              )
              
              val dateText = dateFormatter.format(Date(System.currentTimeMillis()))
              

              这是 Android 上可用的最佳选择,它将根据设备默认定位格式化时间

              示例输出取决于区域设置:

              • 美国 - 02/18/2021, 1:00:00 PM(美国使用 12 格式时间)
              • 乌克兰 - 18.2.2012, 13:00:00(乌克兰使用 24 格式时间)

              所以它会自动为您完成所有艰巨的工作

              附:如果您需要 2021 而不是 21,则将 yy 替换为 yyyy,如果要将月份显示为单词而不是数字,则将 MM 替换为 MMM 等等

              例如"dMMMyyyyjjmmss" for USA 将返回:

              Feb 18, 2021, 1:43:20 PM

              对于俄罗斯:

              18 февр. 2021 г., 13:44:18

              太棒了,不是吗?

              【讨论】:

                猜你喜欢
                • 2018-03-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-11-08
                • 2015-12-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多