我写了下面的代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
我想要当前日期的字符串格式,比如
28-Dec-2011
这样我就可以把它设置为TextView。
我写了下面的代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
我想要当前日期的字符串格式,比如
28-Dec-2011
这样我就可以把它设置为TextView。
您可以使用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);
试试这个,
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
这与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());
}
它是简单的一行代码,用于以yyyy-MM-dd格式获取当前日期 你可以使用你想要的格式:
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
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();
在当前地区(设备地区!)中获取当前日期的最简单方法:
String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
如果你想拥有不同风格的日期,请使用getDateInstance(int style):
DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());
其他样式:DateFormat。长,DateFormat。DATE_FIELD DateFormat。DAY_OF_YEAR_FIELD等(使用CTRL+空格可以看到所有这些)
如果你也需要时间:
String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
工作就像一个魅力和转换为字符串作为奖励;)
SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
String thisDate = currentDate.format(todayDate);
这是我使用的代码:
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);
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//导入Date类为java.util
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());
calendar字段是我的CalendarView。 进口:
import android.widget.CalendarView;
import java.util.Date;
我得到了当前的日期,没有错误。
您可以使用以下代码获得所需格式的日期。
String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new 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 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());
我已经用过这个了:
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) 然后我使用format将日期更改为字符串。
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c);
这是最好的答案……
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);
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();
我提供的是现代的答案。
java。time和ThreeTenABP
获取当前日期:
LocalDate today = LocalDate.now(ZoneId.of("America/Hermosillo"));
这将为您提供一个LocalDate对象,您应该使用该对象在程序中保存日期。LocalDate是一个没有时间的日期。
只有当你需要向用户显示日期时,将其格式化为适合用户语言环境的字符串:
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"));
2019 年 7 月 13 日
我正在使用和推荐java。时间,现代Java日期和时间API。在问题和/或许多其他答案中使用的DateFormat, SimpleDateFormat,日期和日历,设计很差,已经过时了。和java。和时间一起工作真是太好了。
问:我可以使用java吗?Android的时间?
是的,java。time在新旧安卓设备上都能很好地运行。它只需要至少Java 6。
在Java 8及以后版本和更新的Android设备上(从API级别26开始),内置了现代API。 在Java 6和7中获得ThreeTen Backport,现代类的后端口(JSR 310的ThreeTen;参见底部的链接)。 在(旧的)Android上使用ThreeTen Backport的Android版本。叫做ThreeTenABP。并确保从org.three .bp导入带有子包的日期和时间类。
链接
Oracle教程:Date Time解释如何使用java.time。 Java规范请求(JSR) 310,其中Java。时间是最早被描述的。 ThreeTen Backport项目,java的Backport。Java 6和7的时间(JSR-310的ThreeTen)。 ThreeTenABP, Android版的ThreeTen Backport 问:如何在Android项目中使用ThreeTenABP,并有一个非常详细的解释。
只需一行代码获得简单的日期格式:
SimpleDateFormat.getDateInstance().format(Date())
产出:2020年5月18日
SimpleDateFormat.getDateTimeInstance().format(Date())
上午11:00:39
SimpleDateFormat.getTimeInstance().format(Date())
输出:11:00:39 AM
希望这个答案足以得到这个日期和时间格式…:)
在Kotlin
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")}
此方法可用于从系统中获取当前日期。
public static String getCurrentDateAndTime(){
Date c = Calendar.getInstance().getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = simpleDateFormat.format(c);
return formattedDate;
}
我尝试过这种方法,对我很有效。
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())
LocalDateTime ldt2 = LocalDateTime.now();
String year = ldt2.getYear()+"";
String month = ldt2.getMonthValue()+"";
String date = ldt2.getDayOfMonth()+"";
String hour = ldt2.getHour()+"";
String minute = ldt2.getMinute()+"";
String secs = ldt2.getSecond()+"";