如何获取日期格式,如yyyy-mm-dd am in java [英] how to get date format like yyyy-mm-dd am in java

查看:172
本文介绍了如何获取日期格式,如yyyy-mm-dd am in java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得如下日期格式:

I need to get date format like:

"yyyy-mm-dd am"

"yyyy-mm-dd pm"

在Java for Android中

In Java for Android

我不需要当前时间,只需 yyyy-mm-dd 上午 / 下午

I do not need current time, only yyyy-mm-dd plus am/pm

如何直接制作?

我使用以下方法获取当前日期:

i use the method below to get current date:

    private static String getDate(){
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        return sdf.format(c.getTime());
    }


推荐答案

您应该使用以下这些行代码... SimpleDateFormat 只有一个问题,就是当你将设备语言改为另一种语言而不是 SimpleDateFormat 时,字符串也会随着语言的变化而变化可以在您的应用程序中创建问题。因此,如果您希望根据应用程序语言更改而不是使用更改日期字符串,建议您使用下面对你有帮助的概念或者只是简单地使用 SimpleDateFormat 来不将应用程序语言更改为另一个语言。

You should use these below lines of code...There is only one problem with the SimpleDateFormat is that when ever you change the device language to another language than SimpleDateFormat's string also changes with the language which can create problem in yours application..Therefore i suggest you if you do not want to change the date string according the application language changes than Use below concept which become helpful for you Or Just simply use SimpleDateFormat for not changing the application language to another langauge.

     Calendar ci = Calendar.getInstance();
     String AM_PM;

      if(ci.get(Calendar.AM_PM)==0)
        {
             AM_PM ="AM";
        }
        else
        {
             AM_PM ="PM";
        }
        String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" + 
            (ci.get(Calendar.MONTH) + 1) + "-" +
            ci.get(Calendar.DAY_OF_MONTH)+" "+AM_PM;

         System.out.println("time=========================================="+CiDateTime);

输出: -

 time==========================================2015-5-20 PM

这篇关于如何获取日期格式,如yyyy-mm-dd am in java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆