如何在Android中获取当前日期?

时间:2021-06-24 22:34:05

I wrote the following code

我写了下面的代码

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

But is asking me parameter, I want current date in string format,

但是问我参数,我想要字符串格式的当前日期,

like

就像

28-Dec-2011

so that I can set over TextView,

我可以设置TextView,

explain a bit, if you think something is necessary, I am new to Android Development.

解释一下,如果你认为有必要的话,我是Android开发的新手。

20 个解决方案

#1


278  

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

您可以使用SimpleDateFormat类来格式化日期。

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 and here, I would suggest you go through this example and understand the concept of SimpleDateFormat class.

详细的示例在这里和这里,我建议您通过这个示例了解SimpleDateFormat类的概念。

Final Solution:

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

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

#2


102  

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

这是一个简单的一行代码,用于获取当前日期的yyyyyyyy - mm -dd格式,您可以使用您想要的格式:

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

#3


23  

This is nothing to do with android as it is java based so you could use

这与android没有关系,因为它是基于java的,所以您可以使用它

private String getDateTime() { 
   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   Date date = new Date(); 
   return dateFormat.format(date); 
}

#4


11  

 public String giveDate() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
    return sdf.format(cal.getTime());
 }

#5


6  

try this,

试试这个,

SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);

#6


6  

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);

#7


5  

CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

You need an instance first

首先需要一个实例

#8


4  

The below code displays the both time and date

下面的代码显示了时间和日期

Calendar cal = Calendar.getInstance();
cal.getTime().toString();

#9


4  

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

// import Date class as java.util

//导入日期类为java.util

#10


3  

Calendar cal = Calendar.getInstance();      
Calendar dt = Calendar.getInstance(); 
dt.clear();
dt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DATE)); 
return dt.getTime();        

#11


2  

A simple tweak to Paresh's solution:

帕雷什的解决方案有一个简单的调整:

Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);

#12


2  

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

#13


0  

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):

如果您想使用不同样式的日期,请使用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)

其他风格:DateFormat。长,DateFormat。DATE_FIELD DateFormat。DAY_OF_YEAR_FIELD等(使用CTRL+空格查看所有这些)

If you need the time too:

如果你也需要时间:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());

#14


0  

  public static String getDateTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

#15


0  

try with this link of code this is absolute correct answer for all cases all over date and time. or customize date and time as per need and requirement of app.

尝试使用这个代码链接,这是绝对正确的答案,适用于所有的时间和时间。或者根据app的需要和要求定制日期和时间。

try with this link .try with this link

试试这个链接,试试这个链接

#16


0  

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:

“calendar”字段是我的calendar视图。进口:

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

I've got current date without errors.

我现在的日期没有错误。

#17


0  

You can use following code to get a date in the format you want.

您可以使用以下代码获得所需格式的日期。

String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));

#18


0  

This is the code i used:

这是我使用的代码:

             Date date = new Date();  // to get the date
             SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); // getting date in this format
             String formattedDate = df.format(date.getTime());
             text.setText(formattedDate);

#19


0  

I've already used this:

我已经用这个:

Date What_Is_Today=Calendar.getInstance().getTime();
SimpleDateFormat Dateformat = new SimpleDateFormat("dd-MM-yyyy");
String Today=Dateformatf.format(What_Is_Today);

Toast.makeText(this,Today,Toast.LENGTH_LONG).show();

at first I get time, then I declared a Simple Date Format (to get date like: 19-6-2018) then I use format to change date to string.

首先,我声明了一个简单的日期格式(比如:19-6-2018),然后使用Format将日期改为string。

#20


-1  

This is the code I used:

这是我使用的代码:

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

// Set current date into textview
tvDisplayDate.setText(new StringBuilder()
    .append(month + 1).append("-") // Month is 0 based, add 1
    .append(day).append("-")
    .append(year).append("   Today is :" + thursday ) );

// Set current date into datepicker
dpResult.init(year, month, day, null);

#1


278  

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

您可以使用SimpleDateFormat类来格式化日期。

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 and here, I would suggest you go through this example and understand the concept of SimpleDateFormat class.

详细的示例在这里和这里,我建议您通过这个示例了解SimpleDateFormat类的概念。

Final Solution:

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

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

#2


102  

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

这是一个简单的一行代码,用于获取当前日期的yyyyyyyy - mm -dd格式,您可以使用您想要的格式:

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

#3


23  

This is nothing to do with android as it is java based so you could use

这与android没有关系,因为它是基于java的,所以您可以使用它

private String getDateTime() { 
   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   Date date = new Date(); 
   return dateFormat.format(date); 
}

#4


11  

 public String giveDate() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
    return sdf.format(cal.getTime());
 }

#5


6  

try this,

试试这个,

SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);

#6


6  

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);

#7


5  

CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

You need an instance first

首先需要一个实例

#8


4  

The below code displays the both time and date

下面的代码显示了时间和日期

Calendar cal = Calendar.getInstance();
cal.getTime().toString();

#9


4  

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

// import Date class as java.util

//导入日期类为java.util

#10


3  

Calendar cal = Calendar.getInstance();      
Calendar dt = Calendar.getInstance(); 
dt.clear();
dt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DATE)); 
return dt.getTime();        

#11


2  

A simple tweak to Paresh's solution:

帕雷什的解决方案有一个简单的调整:

Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);

#12


2  

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

#13


0  

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):

如果您想使用不同样式的日期,请使用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)

其他风格:DateFormat。长,DateFormat。DATE_FIELD DateFormat。DAY_OF_YEAR_FIELD等(使用CTRL+空格查看所有这些)

If you need the time too:

如果你也需要时间:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());

#14


0  

  public static String getDateTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

#15


0  

try with this link of code this is absolute correct answer for all cases all over date and time. or customize date and time as per need and requirement of app.

尝试使用这个代码链接,这是绝对正确的答案,适用于所有的时间和时间。或者根据app的需要和要求定制日期和时间。

try with this link .try with this link

试试这个链接,试试这个链接

#16


0  

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:

“calendar”字段是我的calendar视图。进口:

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

I've got current date without errors.

我现在的日期没有错误。

#17


0  

You can use following code to get a date in the format you want.

您可以使用以下代码获得所需格式的日期。

String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));

#18


0  

This is the code i used:

这是我使用的代码:

             Date date = new Date();  // to get the date
             SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); // getting date in this format
             String formattedDate = df.format(date.getTime());
             text.setText(formattedDate);

#19


0  

I've already used this:

我已经用这个:

Date What_Is_Today=Calendar.getInstance().getTime();
SimpleDateFormat Dateformat = new SimpleDateFormat("dd-MM-yyyy");
String Today=Dateformatf.format(What_Is_Today);

Toast.makeText(this,Today,Toast.LENGTH_LONG).show();

at first I get time, then I declared a Simple Date Format (to get date like: 19-6-2018) then I use format to change date to string.

首先,我声明了一个简单的日期格式(比如:19-6-2018),然后使用Format将日期改为string。

#20


-1  

This is the code I used:

这是我使用的代码:

final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

// Set current date into textview
tvDisplayDate.setText(new StringBuilder()
    .append(month + 1).append("-") // Month is 0 based, add 1
    .append(day).append("-")
    .append(year).append("   Today is :" + thursday ) );

// Set current date into datepicker
dpResult.init(year, month, day, null);