我写了以下代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
但是问我参数,我想要字符串格式的当前日期,
像
28-Dec-2011
这样我就可以设置了TextView
,
解释一下,如果您认为有必要,我是 Android 开发的新手。
我写了以下代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
但是问我参数,我想要字符串格式的当前日期,
像
28-Dec-2011
这样我就可以设置了TextView
,
解释一下,如果您认为有必要,我是 Android 开发的新手。
您可以使用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);
它简单的一行代码以这种yyyy-MM-dd格式获取当前日期,您可以使用您想要的格式:
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
这与android无关,因为它是基于java的,所以你可以使用
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
public String giveDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
return sdf.format(cal.getTime());
}
试试这个,
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
CharSequence s = DateFormat.getDateInstance().format("MMMM d, yyyy ");
您首先需要一个实例
像魅力一样工作并转换为字符串作为奖励;)
SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
String thisDate = currentDate.format(todayDate);
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// 将 Date 类导入为java.util
对 Paresh 解决方案的简单调整:
Date date = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(date);
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String date = df.format(Calendar.getInstance().getTime());
下面的代码显示时间和日期
Calendar cal = Calendar.getInstance();
cal.getTime().toString();
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();
我正在提供现代答案。
要获取当前日期:
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
,Date
并Calendar
在问题和/或许多其他答案中使用,设计不良且早已过时。而且 java.time 更好用。
是的,java.time 在较旧和较新的 Android 设备上运行良好。它只需要至少Java 6。
org.threeten.bp
子包中导入日期和时间类。java.time
第一次描述的地方。java.time
Java 6 和 7 的反向移植(ThreeTen for JSR-310)。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();
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);
您可以使用以下代码获取所需格式的日期。
String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c);
这是最好的答案...
此方法可用于从系统获取当前日期。
public static String getCurrentDateAndTime(){
Date c = Calendar.getInstance().getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = simpleDateFormat.format(c);
return formattedDate;
}
只需一行代码即可获得简单的日期格式:
SimpleDateFormat.getDateInstance().format(Date())
输出:2020 年 5 月 18 日
SimpleDateFormat.getDateTimeInstance().format(Date())
输出:2020 年 5 月 18 日上午 11:00:39
SimpleDateFormat.getTimeInstance().format(Date())
输出:上午 11:00:39
希望这个答案足以得到这个日期和时间格式...... :)
在当前语言环境(设备语言环境!)中获取当前日期的最简单方法:
String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
如果您想拥有不同样式的日期,请使用getDateInstance(int style)
:
DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());
其他样式:DateFormat.LONG
、DateFormat.DATE_FIELD
、DateFormat.DAY_OF_YEAR_FIELD
等(用于CTRL+Space查看所有样式)
如果你也需要时间:
String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
public static String getDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
return simpleDateFormat.format(date);
}
尝试使用此代码链接,这对于所有日期和时间的所有情况都是绝对正确的答案。或根据应用程序的需要和要求自定义日期和时间。
试试这个链接。试试这个链接
我使用 CalendarView 编写了日历应用程序,这是我的代码:
CalendarView cal = (CalendarView) findViewById(R.id.calendar);
cal.setDate(new Date().getTime());
“日历”字段是我的日历视图。进口:
import android.widget.CalendarView;
import java.util.Date;
我有没有错误的当前日期。
这是我使用的代码:
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);
我已经用过这个:
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)然后我使用格式将日期更改为字符串。
在科特林
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")}
如果您只想获取日期,只需输入这样的代码
Calendar.getInstance().getTime();
尝试使用这种方法对我有用。
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()))
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())
这是我使用的代码:
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);