需要将我的服务器时间戳字符串转换为 java 中 'yyyy-MM-dd' 格式的另一个时区
Need to convert my server timestamp string to another timezone in 'yyyy-MM-dd' format in java
我有一个输入字符串:
billDate="2016-03-16T10:48:59+05:30"(请查看中间的 T)。
现在我想将其转换为另一个时间戳 (America/New_York)。
我的最终结果应该是 2016 年 3 月 16 日或 2016 年 3 月 15 日,具体取决于小时值。
我看到了很多例子,但没有得到如何将上面的长日期时间字符串转换为 America/New_York 的另一个字符串的提示。
有人可以帮我解决这个问题吗?
我试过下面的代码,但它总是给出任何小时值的 3 月 16 日。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) {
String output = formatDate("2016-03-1611T:27:58+05:30");
System.out.println(output);
}
public static String formatDate(String inputDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
Date parsedDate = sdf.parse(inputDate);
return sdf.format(parsedDate);
}
catch (ParseException e) {
// handle exception
}
return null;
}
}
After trying I finally got the code to solve the issue:
The below code works fine:
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class Test {
public static final SimpleDateFormat fDateTime = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
public static void main(String[] args) {
String output = getFormattedDate("2016-03-1611T23:27:58+05:30");
System.out.println(output);
}
public static String getFormattedDate(String inputDate) {
try {
Date dateAfterParsing = fDateTime.parse(inputDate);
fDateTime.setTimeZone(TimeZone.getTimeZone("timeZone"));
return fDateTime.format(dateAfterParsing);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我有一个输入字符串:
billDate="2016-03-16T10:48:59+05:30"(请查看中间的 T)。
现在我想将其转换为另一个时间戳 (America/New_York)。
我的最终结果应该是 2016 年 3 月 16 日或 2016 年 3 月 15 日,具体取决于小时值。
我看到了很多例子,但没有得到如何将上面的长日期时间字符串转换为 America/New_York 的另一个字符串的提示。 有人可以帮我解决这个问题吗?
我试过下面的代码,但它总是给出任何小时值的 3 月 16 日。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) {
String output = formatDate("2016-03-1611T:27:58+05:30");
System.out.println(output);
}
public static String formatDate(String inputDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
Date parsedDate = sdf.parse(inputDate);
return sdf.format(parsedDate);
}
catch (ParseException e) {
// handle exception
}
return null;
}
}
After trying I finally got the code to solve the issue:
The below code works fine:
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class Test {
public static final SimpleDateFormat fDateTime = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
public static void main(String[] args) {
String output = getFormattedDate("2016-03-1611T23:27:58+05:30");
System.out.println(output);
}
public static String getFormattedDate(String inputDate) {
try {
Date dateAfterParsing = fDateTime.parse(inputDate);
fDateTime.setTimeZone(TimeZone.getTimeZone("timeZone"));
return fDateTime.format(dateAfterParsing);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}