将字符串解析为Java Calendar中的ICT时区

huangapple go评论72阅读模式
英文:

Parse String to TimeZone ICT in Calendar java

问题

我正在尝试将一个字符串解析为日历,但是目前在时区(TimeZone)方面遇到问题:
我的代码:

public static Calendar convertStringToFullDates(String dateString) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(PATTENT_FULL_DATE_FORMAT, Locale.US);
    try {
        cal.setTime(sdf.parse(dateString));
    } catch (ParseException e) {
        DebugLog.e(e.getLocalizedMessage());
    }
    return cal;
}

以及字符串:

String str = "Fri May 11 00:00:00 ICT 2018";

和模式:

private static final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";

我尝试了,但是它抛出一个如下的异常:
> 无法解析的日期: "Fri May 11 00:00:00 ICT 2018"

如何解决这个问题?

英文:

I am trying to parse a String into a Calendar but right now I'm having problems at TimeZone:
My code:

public static Calendar convertStringToFullDates(String dateString) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(PATTENT_FULL_DATE_FORMAT, Locale.US);
    try {
        cal.setTime(sdf.parse(dateString));
    } catch (ParseException e) {
        DebugLog.e(e.getLocalizedMessage());
    }
    return cal;
}

and String :

String str = "Fri May 11 00:00:00 ICT 2018";

and pattern:

private static final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";

I tried but it throws an exception like this:

>Unparseable date: "Fri May 11 00:00:00 ICT 2018"

How to solve this problem?

答案1

得分: 1

以下是翻译好的部分:

以下是能够正常运行的代码示例

    public class Main {
        public static void main(String[] args) throws ParseException {
            String str = "Fri May 11 00:00:00 ICT 2018";
            final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(PATTENT_FULL_DATE_FORMAT, Locale.US);
            Date date = sdf.parse(str);
            System.out.println(date);
        }
    }

请注意,`java.util` 日期时间类已经过时且容易出错同样的问题也存在于它们的格式化API `SimpleDateFormat`我建议您完全停止使用它们转而使用[现代日期时间API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html)。

如果您在进行Android项目并且您的Android API级别仍不符合Java-8可以查看[通过解糖实现的Java 8+ API](https://developer.android.com/studio/write/java8-support-table)以及[如何在Android项目中使用ThreeTenABP](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project)。

**使用现代日期时间API**

    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.Locale;
    
    public class Main {
        public static void main(String[] args) {
            String str = "Fri May 11 00:00:00 ICT 2018";
            final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT_FULL_DATE_FORMAT, Locale.US);
            ZonedDateTime zdt = ZonedDateTime.parse(str, dtf);
            System.out.println(zdt);
    
            // 以自定义格式打印日期时间
            System.out.println(zdt.format(dtf));
        }
    }

**输出结果**

    2018-05-11T00:00+07:00[Asia/Bangkok]
    Fri May 11 00:00:00 ICT 2018

**[教程日期时间](https://docs.oracle.com/javase/tutorial/datetime/index.html)**中可以了解更多关于现代日期时间API的内容。
英文:

The following code works for me:

public class Main {
public static void main(String[] args) throws ParseException {
String str = "Fri May 11 00:00:00 ICT 2018";
final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(PATTENT_FULL_DATE_FORMAT, Locale.US);
Date date = sdf.parse(str);
System.out.println(date);
}
}

Note that java.util date-time classes are outdated and error-prone and so is their formatting API, SimpleDateFormat. I suggest you should stop using them completely and switch to the modern date-time API.

If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Using the modern date-time API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String str = "Fri May 11 00:00:00 ICT 2018";
final String PATTENT_FULL_DATE_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(PATTENT_FULL_DATE_FORMAT, Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse(str, dtf);
System.out.println(zdt);
// Print the date-time in a custom format
System.out.println(zdt.format(dtf));
}
}

Output:

2018-05-11T00:00+07:00[Asia/Bangkok]
Fri May 11 00:00:00 ICT 2018

Learn more about the modern date-time API at Trail: Date Time.

huangapple
  • 本文由 发表于 2020年10月20日 15:54:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64440806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定