java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?
我想将date1格式的日期转换为date2格式的日期对象。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.set(2012, 8, 21);
Date date = cal.getTime();
Date date1 = simpleDateFormat.parse(date);
Date date2 = simpleDateFormat.parse(date1);
println date1
println date2
Phoenix asked 2020-08-05T08:07:19Z
9个解决方案
110 votes
使用parse:
DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse("August 21, 2012");
String formattedDate = targetFormat.format(date); // 20120821
还要注意,parse接受的是String,而不是Date的对象,该对象已被解析。
João Silva answered 2020-08-05T08:07:35Z
5 votes
tl;博士
LocalDate.parse(
"January 08, 2017" ,
DateTimeFormatter.ofPattern( "MMMM dd, uuuu" , Locale.US )
).format( DateTimeFormatter.BASIC_ISO_DATE )
使用java.time
“问题”和“其他答案”使用麻烦的旧日期时间类(现已遗留),由java.time类取代。
您具有仅日期的值,因此请使用仅日期的类。 YearQuarter类表示没有日期和时区的仅日期值。
String input = "January 08, 2017";
Locale l = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMMM dd, uuuu" , l );
LocalDate ld = LocalDate.parse( input , f );
所需的输出格式由ISO 8601标准定义。 对于仅日期的值,“扩展”格式为YYYY-MM-DD,例如YearQuarter,而最小化分隔符使用的“基本”格式为YYYYMMDD,例如20170108。
我强烈建议使用扩展格式以提高可读性。 但是,如果您坚持使用基本格式,则该格式器会在名为YearQuarter的YearQuarter类上预定义为常量。
String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE );
看到此代码在IdeOne.com上实时运行。
ld.toString():2017-01-08
输出:20170108
关于java.time
java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧版旧式日期时间类,例如YearQuarter、YearQuarter和YearQuarter。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参见Oracle教程。 并在Stack Overflow中搜索许多示例和说明。 规格为JSR 310。
在哪里获取java.time类?
Java SE 8和SE 9及更高版本内置的
标准Java API的一部分,具有捆绑的实现。
Java 9添加了一些次要功能和修复。
从Java 6结束到7很多java.time功能都在ThreeTen-Backport中反向移植到Java 6和7。
安卓系统ThreeTenABP项目专门针对Android改编了ThreeTen-Backport(如上所述)。
请参阅如何使用ThreeTenABP…。
ThreeTen-Extra项目使用其他类扩展了java.time。 该项目为将来可能在java.time中添加内容提供了一个试验场。 您可能会在这里找到一些有用的类,例如YearQuarter、YearQuarter、YearQuarter等。
Basil Bourque answered 2020-08-05T08:09:36Z
2 votes
使用Java 8,我们可以实现以下目标:
private static String convertDate(String strDate)
{
//for strdate = 2017 July 25
DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("yyyy MMMM dd")
.toFormatter();
LocalDate parsedDate = LocalDate.parse(strDate, f);
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String newDate = parsedDate.format(f2);
return newDate;
}
输出将是:“ 07/25/2017”
KayV answered 2020-08-05T08:10:02Z
1 votes
请参考以下方法。 它以日期字符串作为参数1,您需要指定日期的现有格式作为参数2,结果(预期)格式作为参数3。
请参阅此链接以了解各种格式:可用的日期格式
public static String formatDateFromOnetoAnother(String date,String givenformat,String resultformat) {
String result = "";
SimpleDateFormat sdf;
SimpleDateFormat sdf1;
try {
sdf = new SimpleDateFormat(givenformat);
sdf1 = new SimpleDateFormat(resultformat);
result = sdf1.format(sdf.parse(date));
}
catch(Exception e) {
e.printStackTrace();
return "";
}
finally {
sdf=null;
sdf1=null;
}
return result;
}
ThmHarsh answered 2020-08-05T08:10:26Z
0 votes
希望这会帮助某人。
public static String getDate(
String date, String currentFormat, String expectedFormat)
throws ParseException {
// Validating if the supplied parameters is null
if (date == null || currentFormat == null || expectedFormat == null ) {
return null;
}
// Create SimpleDateFormat object with source string date format
SimpleDateFormat sourceDateFormat = new SimpleDateFormat(currentFormat);
// Parse the string into Date object
Date dateObj = sourceDateFormat.parse(date);
// Create SimpleDateFormat object with desired date format
SimpleDateFormat desiredDateFormat = new SimpleDateFormat(expectedFormat);
// Parse the date into another format
return desiredDateFormat.format(dateObj).toString();
}
Dinesh Lomte answered 2020-08-05T08:10:46Z
0 votes
试试这个
这是将一种日期格式更改为另一种日期的最简单方法
public String changeDateFormatFromAnother(String date){
@SuppressLint("SimpleDateFormat") DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@SuppressLint("SimpleDateFormat") DateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy");
String resultDate = "";
try {
resultDate=outputFormat.format(inputFormat.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
Sunil answered 2020-08-05T08:11:10Z
0 votes
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
String fromDateFormat = "dd/MM/yyyy";
String fromdate = 15/03/2018; //Take any date
String CheckFormat = "dd MMM yyyy";//take another format like dd/MMM/yyyy
String dateStringFrom;
Date DF = new Date();
try
{
//DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
FromDF.setLenient(false); // this is important!
Date FromDate = FromDF.parse(fromdate);
dateStringFrom = new
SimpleDateFormat(CheckFormat).format(FromDate);
DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
DF=FromDF1.parse(dateStringFrom);
System.out.println(dateStringFrom);
}
catch(Exception ex)
{
System.out.println("Date error");
}
output:- 15/03/2018
15 Mar 2018
jalpa jogi answered 2020-08-05T08:11:26Z
0 votes
Kotlin相当于João Silva回答的答案
fun getFormattedDate(originalFormat: SimpleDateFormat, targetFormat: SimpleDateFormat, inputDate: String): String {
return targetFormat.format(originalFormat.parse(inputDate))
}
用法(在Android中):
getFormattedDate(
SimpleDateFormat(FormatUtils.d_MM_yyyy, Locale.getDefault()),
SimpleDateFormat(FormatUtils.d_MMM_yyyy, Locale.getDefault()),
dateOfTreatment
)
注意:常数值:
// 25 Nov 2017
val d_MMM_yyyy = "d MMM yyyy"
// 25/10/2017
val d_MM_yyyy = "d/MM/yyyy"
Killer answered 2020-08-05T08:11:55Z
-1 votes
//Convert input format 19-FEB-16 01.00.00.000000000 PM to 2016-02-19 01.00.000 PM
SimpleDateFormat inFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS aaa");
Date today = new Date();
Date d1 = inFormat.parse("19-FEB-16 01.00.00.000000000 PM");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd hh.mm.ss.SSS aaa");
System.out.println("Out date ="+outFormat.format(d1));
Karthik M answered 2020-08-05T08:12:10Z