English 中文(简体)
How to parse time stamps with Unicode characters in Java or Perl?
原标题:

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 u4e0bu5348 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 u4e0bu5348 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
问题回答

Try this:

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class ts {
public static void main(final String[] args) {
    String installTime = "2009/11/26 u4e0bu5348 04:40:54";
    Locale[] locales = DateFormat.getAvailableLocales();
    for (Locale locale : locales) {
        try {
            Date instTime = DateFormat.getDateInstance(DateFormat.LONG, locale).parse(
                    installTime);
            System.out.println("BINGO! Worked with " + locale);
            System.out.println(instTime);
        } catch (Exception ex) {
        }
    }
}
}

Output:

BINGO! Worked with ja_JP
Thu Nov 26 00:00:00 GMT 2009
BINGO! Worked with ja
Thu Nov 26 00:00:00 GMT 2009

No, U+FFFF is not a valid character in any plane, so your string "installTime" contains undefined garbage.

Edit: furthermore, the code you posted is not the code you re running, since the code you posted (properly) results in

java.text.ParseException: Unparseable date: "2009/11/26 ~K~M~H 04:40:54"




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签