简体   繁体   中英

How to get the current system date in a string - ANDROID

How can i get the current date in a string in Android?

I know that it's gettable using

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

But i get it as 25-Mar-2014, and what i want is 25-3-2014. Thanks.

Try this..

Change dd-MMM-yyyy to dd-MM-yyyy

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

Use

String date = new SimpleDateFormat("yyyy-MM-dd")
                            .format(new Date());

What you are passing for DateFormat dd-MMM-yyyy will gives you month in words. Change it to dd-MM-yyyy

 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
 String date = sdf.format(new Date(System.currentTimeMillis()));

You should use the date format as SimpleDateFormat("dd-MM-yyyy"); not like SimpleDateFormat("dd-MMM-yyyy"); . Here is the doc for SimpledateFormat

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

Simple change!

Use#

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");

Instead#

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
   String dateInString = "7-Jun-2013";
try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

} catch (ParseException e) {
    e.printStackTrace();
}
Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = sdf.format(date);

Here is some format to get the desire date.

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
           yyyy-MM-dd HH:mm 1969-12-31 16:00
           yyyy-MM-dd HH:mm 1970-01-01 00:00
          yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
          yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
   yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
   yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

This is surprisingly the only answer which does exactly what the OP wants (okay a very small subtlety):

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
String formatDate = sdf.format(Calendar.getInstance().getTime());
System.out.println(formatDate);

Note, that there is only one pattern symbol M, not two! So the output is not

25-03-2014

but

25-3-2014 (what was explicitly asked for)

try this one...


Calendar c = Calendar.getInstance();
    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    String formatDate = df.format(c.getTime());

    System.out.print(formatDate);


if you want to change vice versa means follow below  steps..

    int selectedYear = 2013;
    int selectedDay = 20;
    int selectedMonth = 11;

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, selectedYear);
    cal.set(Calendar.DAY_OF_MONTH, selectedDay);
    cal.set(Calendar.MONTH, selectedMonth);
    String format = new SimpleDateFormat("E, MMM d, yyyy").format(cal.getTime());


or else

DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);

or 

GregorianCalendar cal = new GregorianCalendar();
        int y, m, d, a;

        y = cal.get(Calendar.YEAR);
        m = cal.get(Calendar.MONTH) + 1;
        d = cal.get(Calendar.DAY_OF_MONTH);
        cal.set(_year, _month, _day);
 String format = new SimpleDateFormat("E, MMM d, yyyy").format(cal.getTime());

hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM