193

我写了以下代码

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

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

28-Dec-2011

这样我就可以设置了TextView

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

4

30 回答 30

422

您可以使用SimpleDateFormat该类以所需的格式格式化日期。

只需检查此链接即可了解您的示例。

例如:

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

更新:

详细的例子在这里,我建议你通过这个例子来理解 SimpleDateFormat 类的概念。

最终解决方案:

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);
于 2011-12-28T11:02:47.630 回答
171

它简单的一行代码以这种yyyy-MM-dd格式获取当前日期,您可以使用您想要的格式:

String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
于 2013-03-29T06:53:49.077 回答
31

这与android无关,因为它是基于java的,所以你可以使用

private String getDateTime() { 
   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   Date date = new Date(); 
   return dateFormat.format(date); 
}
于 2011-12-28T11:04:35.590 回答
12
 public String giveDate() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
    return sdf.format(cal.getTime());
 }
于 2011-12-28T11:23:07.830 回答
11

试试这个,

SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
于 2011-12-28T11:03:52.087 回答
8
CharSequence s  = DateFormat.getDateInstance().format("MMMM d, yyyy ");

您首先需要一个实例

于 2011-12-28T11:02:25.040 回答
8

像魅力一样工作并转换为字符串作为奖励;)

SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
      Date todayDate = new Date();
    String thisDate = currentDate.format(todayDate);
于 2015-09-24T02:33:16.793 回答
7
 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

// 将 Date 类导入为java.util

于 2016-02-02T15:28:06.463 回答
5

对 Paresh 解决方案的简单调整:

Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);
于 2018-02-09T06:31:20.540 回答
5
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(Calendar.getInstance().getTime());
于 2018-02-23T17:37:56.213 回答
4

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

Calendar cal = Calendar.getInstance();
cal.getTime().toString();
于 2011-12-28T11:11:33.010 回答
4
 public static String getcurrentDateAndTime(){

        Date c = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String formattedDate = simpleDateFormat.format(c);
        return formattedDate;
    }

// String currentdate=  getcurrentDateAndTime();
于 2019-07-13T09:36:15.543 回答
4

我正在提供现代答案。

java.time 和 ThreeTenABP

要获取当前日期:

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

这为您提供了一个LocalDate对象,您应该使用该对象在程序中保存日期。ALocalDate是没有时间的日期。

只有当您需要向用户显示日期时,才将其格式化为适合用户语言环境的字符串:

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

当我今天在美国英语语言环境中运行此代码段时,输出为:

2019 年 7 月 13 日

如果您希望它更短,请指定FormatStyle.MEDIUM甚至FormatStyle.SHORT. DateTimeFormatter.ofLocalizedDate使用默认的格式化语言环境,所以关键是它会给出适合该语言环境的输出,对于不同的语言环境是不同的。

如果您的用户对输出格式有非常特殊的要求,请使用格式模式字符串:

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

13-يول-2019

我正在使用并推荐 java.time,它是现代 Java 日期和时间 API。DateFormat, SimpleDateFormat,DateCalendar在问题和/或许多其他答案中使用,设计不良且早已过时。而且 java.time 更好用。

问题:我可以在 Android 上使用 java.time 吗?

是的,java.time 在较旧和较新的 Android 设备上运行良好。它只需要至少Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,现代类的后向端口(ThreeTen 用于 JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

链接

于 2019-07-13T15:17:48.093 回答
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();        
于 2013-09-30T12:09:30.710 回答
3
Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + (month + 1) + "/" + year;

Log.i("TAG", "--->" + date);
于 2019-05-17T06:21:32.563 回答
2

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

String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));
于 2017-05-05T09:37:00.283 回答
2
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);

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

这是最好的答案...

于 2018-09-26T10:16:44.203 回答
2

此方法可用于从系统获取当前日期。

public static String getCurrentDateAndTime(){
    Date c = Calendar.getInstance().getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    String formattedDate = simpleDateFormat.format(c);
    return formattedDate;
}
于 2021-02-10T11:59:35.207 回答
1

只需一行代码即可获得简单的日期格式:

SimpleDateFormat.getDateInstance().format(Date())

输出:2020 年 5 月 18 日

SimpleDateFormat.getDateTimeInstance().format(Date())

输出:2020 年 5 月 18 日上午 11:00:39

SimpleDateFormat.getTimeInstance().format(Date())

输出:上午 11:00:39

希望这个答案足以得到这个日期和时间格式...... :)

于 2020-05-18T12:27:08.943 回答
0

在当前语言环境(设备语言环境!)中获取当前日期的最简单方法:

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

如果您想拥有不同样式的日期,请使用getDateInstance(int style)

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

其他样式:DateFormat.LONGDateFormat.DATE_FIELDDateFormat.DAY_OF_YEAR_FIELD等(用于CTRL+Space查看所有样式)

如果你也需要时间:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
于 2014-11-05T12:12:50.263 回答
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);
    }
于 2016-10-19T10:23:38.753 回答
0

尝试使用此代码链接,这对于所有日期和时间的所有情况都是绝对正确的答案。或根据应用程序的需要和要求自定义日期和时间。

试试这个链接。试试这个链接

于 2016-12-07T18:38:07.737 回答
0

我使用 CalendarView 编写了日历应用程序,这是我的代码:

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

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

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

我有没有错误的当前日期。

于 2017-01-11T14:28:57.713 回答
0

这是我使用的代码:

             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);
于 2017-06-06T11:17:56.673 回答
0

我已经用过这个:

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

起初我得到时间,然后我声明了一个简单的日期格式(获取日期,如:19-6-2018)然后我使用格式将日期更改为字符串。

于 2018-06-19T18:29:04.883 回答
0

在科特林

https://www.programiz.com/kotlin-programming/examples/current-date-time

fun main(args: Array<String>) {

val current = LocalDateTime.now()

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val formatted = current.format(formatter)

println("Current Date and Time is: $formatted")}
于 2020-11-04T11:02:32.997 回答
0

如果您只想获取日期,只需输入这样的代码

Calendar.getInstance().getTime();
于 2021-05-11T07:48:43.767 回答
0

尝试使用这种方法对我有用。

val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault()) // pass the format pattern that you like and done.
println(df.format(Date()))
于 2021-11-25T06:46:40.610 回答
0
In Kotlin you can use this code : - 

Simple only need to change date format to this "dd-MM-yyyy" 
val d = Date()
val str: CharSequence = DateFormat.format("dd-MM-yyyy", d.getTime())
Log.e("", str.toString())
    
In Java You use this code: - 
    
Date date = new Date();
CharSequence str  = DateFormat.format("dd-MM-yyyy", date.getTime());
Log.e("Date", str.toString())
于 2022-01-05T17:01:18.223 回答
-1

这是我使用的代码:

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);
于 2015-10-15T14:02:15.743 回答