How can I get current date in Android?

前端 未结 26 1917
[愿得一人]
[愿得一人] 2020-11-27 10:24

I wrote the following code

Date d = new Date();
CharSequence s  = DateFormat.format(\"MMMM d, yyyy \", d.getTime());

But is asking me para

相关标签:
26条回答
  • 2020-11-27 11:04

    The simplest way to get the current date in current locale (device locale!) :

    String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
    

    If you want to have the date in different styles use getDateInstance(int style):

    DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());
    

    Other styles: DateFormat.LONG, DateFormat.DATE_FIELD, DateFormat.DAY_OF_YEAR_FIELD, etc. (use CTRL+Space to see all of them)

    If you need the time too:

    String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
    
    0 讨论(0)
  • 2020-11-27 11:05

    I am providing the modern answer.

    java.time and ThreeTenABP

    To get the current date:

        LocalDate today = LocalDate.now(ZoneId.of("America/Hermosillo"));
    

    This gives you a LocalDate object, which is what you should use for keeping a date in your program. A LocalDate is a date without time of day.

    Only when you need to display the date to a user, format it into a string suitable for the user’s locale:

        DateTimeFormatter userFormatter
                = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        System.out.println(today.format(userFormatter));
    

    When I ran this snippet today in US English locale, output was:

    July 13, 2019

    If you want it shorter, specify FormatStyle.MEDIUM or even FormatStyle.SHORT. DateTimeFormatter.ofLocalizedDate uses the default formatting locale, so the point is that it will give output suitable for that locale, different for different locales.

    If your user has very special requirements for the output format, use a format pattern string:

        DateTimeFormatter userFormatter = DateTimeFormatter.ofPattern(
                "d-MMM-u", Locale.forLanguageTag("ar-AE"));
    

    13-يول-2019

    I am using and recommending java.time, the modern Java date and time API. DateFormat, SimpleDateFormat, Date and Calendar used in the question and/or many of the other answers, are poorly designed and long outdated. And java.time is so much nicer to work with.

    Question: Can I use java.time on Android?

    Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-11-27 11:06

    You can use the SimpleDateFormat class for formatting date in your desired format.

    Just check this link where you get an idea for your example.

    For example:

    String dateStr = "04/05/2010"; 
     
    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
    Date dateObj = curFormater.parse(dateStr); 
    SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 
     
    String newDateStr = postFormater.format(dateObj); 
    

    Update:

    The detailed example is here, I would suggest you go through this example and understand the concept of SimpleDateFormat class.

    Final Solution:

    Date c = Calendar.getInstance().getTime();
    System.out.println("Current time => " + c);
    
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
    String formattedDate = df.format(c);
    
    0 讨论(0)
  • 2020-11-27 11:06

    I wrote calendar app using CalendarView and it's my code:

    CalendarView cal = (CalendarView) findViewById(R.id.calendar);
    cal.setDate(new Date().getTime());
    

    'calendar' field is my CalendarView. Imports:

    import android.widget.CalendarView;
    import java.util.Date;
    

    I've got current date without errors.

    0 讨论(0)
  • 2020-11-27 11:07

    Works like a charm and converts to String as a bonus ;)

    SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
          Date todayDate = new Date();
        String thisDate = currentDate.format(todayDate);
    
    0 讨论(0)
  • 2020-11-27 11:08

    Its simple one line code for get current Date in this yyyy-MM-dd format you can use your format that you want :

    String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
    
    0 讨论(0)
提交回复
热议问题