日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java中如何把时间封装成类,java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?...

發布時間:2023/11/27 生活经验 40 豆豆

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

總結

以上是生活随笔為你收集整理的java中如何把时间封装成类,java-如何在不使用任何不推荐使用的类的情况下将日期从一种格式转换为另一种格式的日期对象?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。