如何在Java或Perl中使用Unicode字符解析时间戳? [英] How to parse time stamps with Unicode characters in Java or Perl?

查看:57
本文介绍了如何在Java或Perl中使用Unicode字符解析时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的代码尽可能通用.我正在尝试分析产品安装的安装时间.产品中将有两个文件,一个文件带有我需要解析的时间戳,另一个文件说明了安装的语言.

这就是我解析时间戳记的方式

public class ts {
    public static void main (String[] args){
    String installTime = "2009/11/26 \u4e0b\u5348 04:40:54";
    //This timestamp I got from the first file. Those unicode charecters are some Chinese charecters...AM/PM I guess
    //Locale = new Locale();//don't set the language yet
    SimpleDateFormat df = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT);
    Date instTime = null;
    try {
        instTime = df.parse(installTime);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        System.out.println(instTime.toString());
    }
}

我得到的输出是

       Parsing Failed
    java.text.ParseException: Unparseable date: "2009/11/26 \u4e0b\u5348 04:40:54"
     at java.text.DateFormat.parse(Unknown Source)
     at ts.main(ts.java:39)
    Exception in thread "main" java.lang.NullPointerException
     at ts.main(ts.java:45)

它抛出异常,并且在我打印它的末尾,它显示了正确的日期……不过是错误的.如果您能就这些疑问向我澄清,我将不胜感激

  1. 如果这不是正确的方法,如何解析具有unicode字符的时间戳?

  2. 如果解析失败,那么instTime如何能够保留某个日期,但是会出错吗? 我知道它有一些中文,韩文的时间戳,因此我将语言环境设置为zh和ko,如下所示.

    Locale = new Locale("ko");

    Locale = new Locale("ja");

    Locale = new Locale("zh");

我如何在Perl中做同样的事情?我不能使用Date :: Manip包;还有其他办法吗?

解决方案

您的示例日期时间戳不是符合CLDR ,因此我们必须手动定义模式.

use utf8;
use DateTime::Format::CLDR ();

my $cldr = DateTime::Format::CLDR->new(
    locale   => 'zh_CN',
    pattern  => 'yyyy/MM/dd a HH:mm:ss',
    on_error => 'croak',
);

$cldr->parse_datetime('2009/11/26 下午 04:40:54'); # returns a DateTime object

I'm trying to make my code as generic as possible. I'm trying to parse install time of a product installation. I will have two files in the product, one that has time stamp I need to parse and other file tells the language of the installation.

This is how I'm parsing the timestamp

public class ts {
    public static void main (String[] args){
    String installTime = "2009/11/26 \u4e0b\u5348 04:40:54";
    //This timestamp I got from the first file. Those unicode charecters are some Chinese charecters...AM/PM I guess
    //Locale = new Locale();//don't set the language yet
    SimpleDateFormat df = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT);
    Date instTime = null;
    try {
        instTime = df.parse(installTime);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        System.out.println(instTime.toString());
    }
}

The output I get is

       Parsing Failed
    java.text.ParseException: Unparseable date: "2009/11/26 \u4e0b\u5348 04:40:54"
     at java.text.DateFormat.parse(Unknown Source)
     at ts.main(ts.java:39)
    Exception in thread "main" java.lang.NullPointerException
     at ts.main(ts.java:45)

It throws exception and at the end when I print it, it shows some proper date... wrong though. I would really appreciate if you could clarify me on these doubts

  1. How to parse timestamps that have unicode characters if this is not the proper way?

  2. If parsing is failed, how could instTime able to hold some date, wrong though? I know its some chinese,Korean time stamps so I set the locale to zh and ko as follows.. even then same error comes again

    Locale = new Locale("ko");

    Locale = new Locale("ja");

    Locale = new Locale("zh");

How can I do the same thing in Perl? I can't use Date::Manip package; Is there any other way?

解决方案

Your example datetime stamp is not conforming to CLDR, so we have to define a pattern manually.

use utf8;
use DateTime::Format::CLDR ();

my $cldr = DateTime::Format::CLDR->new(
    locale   => 'zh_CN',
    pattern  => 'yyyy/MM/dd a HH:mm:ss',
    on_error => 'croak',
);

$cldr->parse_datetime('2009/11/26 下午 04:40:54'); # returns a DateTime object

这篇关于如何在Java或Perl中使用Unicode字符解析时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆