首页 > 编程知识 正文

java怎样转换日期格式,java时间戳转换为时间格式

时间:2023-05-06 04:32:57 阅读:259991 作者:2575

I have the below code in which date come as string type and I have to set it in US format

so below I have shown it

private static final SimpleDateFormat usOutputDate = new SimpleDateFormat("MM/dd/yyyy");

and inside a method I have written the below code and inside dealDateString the value is coming as 02-Mar-2015

// dealDateString = 02-Mar-2015

java.util.Date dealDate = extractDate(dealDateString);

//here its value get converted in US format that is 03/02/2015

String dd = usOutputDate.format(dealDate);

// ***issue comes here as it get back converted in US format ***

java.util.Date date = format.parse(dd);

brokerInvoiceLineItem.setDealDate(new Date(date));

as shown in the above code that's it value inside String dd is 03/02/2015 but the issue comes at format variable where

its value get converted back in UK format which i do not want please advise how can i convert it in UK format as it

is already converted in US format previously stored inside String dd.

解决方案

try this...

try {

//parsing date format

String dealDateString = "02-Mar-2015";

Date date = formatter.parse(dealDateString);

TimeZone tz = TimeZone.getDefault();

//converting date format for US

TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");

sdfAmerica.setTimeZone(tzInAmerica);

String sDateInAmerica = sdfAmerica.format(date); // Convert to String first

} catch (Exception bug) {

bug.printStackTrace();

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。