如何使用SimpleDateFormat显示当前日期?

发布于 2024-12-16 21:37:49 字数 369 浏览 1 评论 0原文

我正在尝试格式化当前日期和时间,这是我的代码:

Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);

出了问题,因为我收到了这个字符串:19700103-0217

该日期不正确,我应该得到类似的内容这个:20111118-1217

如何解决这个问题?

i'm trying to get the current date and time formatted, this is my code:

Date date = new Date();
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd-HHmm");
String formattedDate=""+FORMATTER.format(date);
Log.d("XXxxxXxXXXXxxxXXXxXxxxX",formattedDate);

Something is going wrong because i'm getting this string: 19700103-0217

That date is incorrect, I should get something like this: 20111118-1217

How to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

复古式 2024-12-23 21:37:49

问题是您的日期不正确。很难说它是如何发生的,因为你还没有表明它来自哪里,但这肯定是问题所在。要确认这一点,请使用内置的 Date.toString() 方法 - 该方法使用系统默认时区,因此您需要小心,但您至少应该看到正确的日期等。

Log.d("date.toString() - ", date.toString());

The problem is that your date is incorrect. It's hard to say how, as you haven't shown where it's coming from, but that's pretty much bound to be the problem. To confirm this, use the built-in Date.toString() method - that uses the system default time zone, so you need to be careful, but you should at least see the right date etc.

Log.d("date.toString() - ", date.toString());
暖伴 2024-12-23 21:37:49

我想说的是,您执行代码的设备(或模拟器)的日期不正确。您的代码应该按预期工作。

无论如何,它与 SimpleDateFormat 无关,只要您使用它的方式是正确的。

I would say the device (or emulator) on which you execute your code has an incorrect date. Your code should work as expected.

In any case, it's not related to SimpleDateFormat, ythe way you use it is correct.

伏妖词 2024-12-23 21:37:49

如果您的目标只是显示当前日期,那么您可以使用日历

使用日历,很容易获得所需格式的日期。

例如:

Calendar cal=Calendar.getInstance();
String year=String.format("%1$tY",cal);
String month=String.format("%1$tm",cal);
String day=String.format("%1$td",cal);

String hour=String.format("%1$tH",cal); // hour in 24 hour time format
String min=String.format("%1$M",cal);

String finalDateString=year+month+day+"-"+hour+min; //gives you in format like 20111118-1217

有关更多甲酸盐,请参阅

If your goal is just to show current date then you can use Calendar though.

Using calendar,its pretty easy to get the date in desired format.

Ex:

Calendar cal=Calendar.getInstance();
String year=String.format("%1$tY",cal);
String month=String.format("%1$tm",cal);
String day=String.format("%1$td",cal);

String hour=String.format("%1$tH",cal); // hour in 24 hour time format
String min=String.format("%1$M",cal);

String finalDateString=year+month+day+"-"+hour+min; //gives you in format like 20111118-1217

For more formates,refer this.

倾`听者〃 2024-12-23 21:37:49

获取当前日期。试试这个

  Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    String formattedDate = df.format(c.getTime());

To get Current Date. Try this

  Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    String formattedDate = df.format(c.getTime());
几度春秋 2024-12-23 21:37:49
Calendar calendar=Calendar.getInstance();

//这将为您提供当前月份。

日期 date = calendar.getTime();
SimpleDateFormat 格式 = new SimpleDateFormat("MMM");

    String currentMonth = format.format(date);

//这将为您提供该月的当前日期。

SimpleDateFormat format1 = new SimpleDateFormat("dd");
字符串 currentDate = format1.format(date);

如果您需要其他格式,可以看这个链接:
http://developer.android.com/reference/java/text/SimpleDateFormat.html

Calendar calendar=Calendar.getInstance();

//This will get you the current month.

Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("MMM");

    String currentMonth = format.format(date);

//This will get you the current date of the month.

SimpleDateFormat format1 = new SimpleDateFormat("dd");
String currentDate = format1.format(date);

If you need other formats, you can see this link:
http://developer.android.com/reference/java/text/SimpleDateFormat.html

半山落雨半山空 2024-12-23 21:37:49

好吧,我可以帮助你,但我有自己的代码。如果你没有得到什么就问我。

首先,你必须创建一个任意名称的类,例如 date.class 并编写代码:

public class Date{
    private Date dt;
    private String stDt;
    private String enDt;

    SRSDDate(){
        dt=new Date();
        int mn;
        int yr;

        mn=dt.getMonth();
        yr=dt.getYear();
        mn=mn+1;
        yr=1900+yr;

        stDt="1/"+mn+"/"+yr;

        switch(yr%4){
            case 0:
                if(mn==2){
                    enDt="29/"+mn+"/"+yr;
                }
                else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
                    enDt="31/"+mn+"/"+yr;
                }
                else{
                    enDt="30/"+mn+"/"+yr;
                }
            break;
            default:
                if(mn==2){
                    enDt="28/"+mn+"/"+yr;
                }
                else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
                    enDt="31/"+mn+"/"+yr;
                }
                else{
                    enDt="30/"+mn+"/"+yr;
                }
            break;
        }
    }
    public String getTodayDate(){
        return (dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate();
    }
    public String getTodayDisplayDate(){
        return dt.getDate()+"/"+(dt.getMonth()+1)+"/"+(dt.getYear()+1900);
    }
    public String getStDt(){
        return stDt;
    }

    public String getEnDt(){
        return enDt;
    }
}

创建类后,你必须声明这一点:

Calendar FromDateCal=Calendar.getInstance();
DatePickerDialog.OnDateSetListener fd=new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
            FromDateCal.set(Calendar.YEAR, year);
            FromDateCal.set(Calendar.MONTH, monthOfYear);
            FromDateCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateFromDate();
        }
};

现在找到要在其中显示日期的编辑框,如下所示:

Date dt=new Date();
FromDateText=(EditText)findViewById(R.id.FromDateText);
FromDateText.setText(dt.getStDt());

现在你有在编辑框中显示日期如下:

private void updateFromDate(){
        int iDay;
        int iMonth;
        int iYear;

        iDay=FromDateCal.get(Calendar.DATE);
        iMonth=FromDateCal.get(Calendar.MONTH);
        iMonth=iMonth+1;
        iYear=FromDateCal.get(Calendar.YEAR);

        String sFDate=iDay+"/"+iMonth+"/"+iYear;
        FromDateText.setText(sFDate);
}

我希望你得到我的答案。

Well, I can help you out but I have my own code. Ask me if you don't get something.

First of all you have to make a class of any name like date.class and write the code:

public class Date{
    private Date dt;
    private String stDt;
    private String enDt;

    SRSDDate(){
        dt=new Date();
        int mn;
        int yr;

        mn=dt.getMonth();
        yr=dt.getYear();
        mn=mn+1;
        yr=1900+yr;

        stDt="1/"+mn+"/"+yr;

        switch(yr%4){
            case 0:
                if(mn==2){
                    enDt="29/"+mn+"/"+yr;
                }
                else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
                    enDt="31/"+mn+"/"+yr;
                }
                else{
                    enDt="30/"+mn+"/"+yr;
                }
            break;
            default:
                if(mn==2){
                    enDt="28/"+mn+"/"+yr;
                }
                else if(mn==1||mn==3||mn==5||mn==7||mn==8||mn==10||mn==12){
                    enDt="31/"+mn+"/"+yr;
                }
                else{
                    enDt="30/"+mn+"/"+yr;
                }
            break;
        }
    }
    public String getTodayDate(){
        return (dt.getYear()+1900)+"-"+(dt.getMonth()+1)+"-"+dt.getDate();
    }
    public String getTodayDisplayDate(){
        return dt.getDate()+"/"+(dt.getMonth()+1)+"/"+(dt.getYear()+1900);
    }
    public String getStDt(){
        return stDt;
    }

    public String getEnDt(){
        return enDt;
    }
}

After making the class, you have to declare this:

Calendar FromDateCal=Calendar.getInstance();
DatePickerDialog.OnDateSetListener fd=new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
            FromDateCal.set(Calendar.YEAR, year);
            FromDateCal.set(Calendar.MONTH, monthOfYear);
            FromDateCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateFromDate();
        }
};

Now find the edit box in which you want to display the date like:

Date dt=new Date();
FromDateText=(EditText)findViewById(R.id.FromDateText);
FromDateText.setText(dt.getStDt());

Now you have to display date on edit box as follows:

private void updateFromDate(){
        int iDay;
        int iMonth;
        int iYear;

        iDay=FromDateCal.get(Calendar.DATE);
        iMonth=FromDateCal.get(Calendar.MONTH);
        iMonth=iMonth+1;
        iYear=FromDateCal.get(Calendar.YEAR);

        String sFDate=iDay+"/"+iMonth+"/"+iYear;
        FromDateText.setText(sFDate);
}

I hope you got my answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文