java中如何把时间封装成类,java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?...
java-如何在不使用任何不推薦使用的類的情況下將日期從一種格式轉(zhuǎn)換為另一種格式的日期對象?
我想將date1格式的日期轉(zhuǎn)換為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個(gè)解決方案
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
“問題”和“其他答案”使用麻煩的舊日期時(shí)間類(現(xiàn)已遺留),由java.time類取代。
您具有僅日期的值,因此請使用僅日期的類。 YearQuarter類表示沒有日期和時(shí)區(qū)的僅日期值。
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標(biāo)準(zhǔn)定義。 對于僅日期的值,“擴(kuò)展”格式為YYYY-MM-DD,例如YearQuarter,而最小化分隔符使用的“基本”格式為YYYYMMDD,例如20170108。
我強(qiáng)烈建議使用擴(kuò)展格式以提高可讀性。 但是,如果您堅(jiān)持使用基本格式,則該格式器會在名為YearQuarter的YearQuarter類上預(yù)定義為常量。
String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE );
看到此代碼在IdeOne.com上實(shí)時(shí)運(yùn)行。
ld.toString():2017-01-08
輸出:20170108
關(guān)于java.time
java.time框架內(nèi)置于Java 8及更高版本中。 這些類取代了麻煩的舊版舊式日期時(shí)間類,例如YearQuarter、YearQuarter和YearQuarter。
現(xiàn)在處于維護(hù)模式的Joda-Time項(xiàng)目建議遷移到j(luò)ava.time類。
要了解更多信息,請參見Oracle教程。 并在Stack Overflow中搜索許多示例和說明。 規(guī)格為JSR 310。
在哪里獲取java.time類?
Java SE 8和SE 9及更高版本內(nèi)置的
標(biāo)準(zhǔn)Java API的一部分,具有捆綁的實(shí)現(xiàn)。
Java 9添加了一些次要功能和修復(fù)。
從Java 6結(jié)束到7很多java.time功能都在ThreeTen-Backport中反向移植到Java 6和7。
安卓系統(tǒng)ThreeTenABP項(xiàng)目專門針對Android改編了ThreeTen-Backport(如上所述)。
請參閱如何使用ThreeTenABP…。
ThreeTen-Extra項(xiàng)目使用其他類擴(kuò)展了java.time。 該項(xiàng)目為將來可能在java.time中添加內(nèi)容提供了一個(gè)試驗(yàn)場。 您可能會在這里找到一些有用的類,例如YearQuarter、YearQuarter、YearQuarter等。
Basil Bourque answered 2020-08-05T08:09:36Z
2 votes
使用Java 8,我們可以實(shí)現(xiàn)以下目標(biāo):
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
請參考以下方法。 它以日期字符串作為參數(shù)1,您需要指定日期的現(xiàn)有格式作為參數(shù)2,結(jié)果(預(yù)期)格式作為參數(shù)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
試試這個(gè)
這是將一種日期格式更改為另一種日期的最簡單方法
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相當(dāng)于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
)
注意:常數(shù)值:
// 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
總結(jié)
以上是生活随笔為你收集整理的java中如何把时间封装成类,java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个性签名主动
- 下一篇: 如何理解这位包子男?[已扎口]