如何在Java或Perl中使用Unicode字符解析时间戳?
我试图使我的代码尽可能通用。我试图解析产品安装的安装时间。我将在产品中有两个文件,一个有我需要解析的时间戳,另一个文件告诉我安装的语言 这就是我解析时间戳的方式如何在Java或Perl中使用Unicode字符解析时间戳?,java,perl,parsing,timestamp,Java,Perl,Parsing,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 fil
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)
解析失败
java.text.ParseException:无法解析的日期:“2009/11/26\u4e0b\u5348 04:40:54”
位于java.text.DateFormat.parse(未知源)
在ts.main(ts.java:39)
线程“main”java.lang.NullPointerException中出现异常
在ts.main(ts.java:45)
它抛出异常,在我打印它的最后,它显示了一些正确的日期。。。但我错了。如果你能澄清我的疑问,我将不胜感激
java.text.ParseException: Unparseable date: "2009/11/26 ~K~M~H 04:40:54"
不,在任何平面中,因此字符串“installTime”包含未定义的垃圾
编辑:此外,您发布的代码不是您正在运行的代码,因为您发布(正确)的代码会导致
java.text.ParseException: Unparseable date: "2009/11/26 ~K~M~H 04:40:54"
试试这个:
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 \u4e0b\u5348 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) {
}
}
}
}
输出:
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
试试这个:
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 \u4e0b\u5348 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) {
}
}
}
}
输出:
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
您的示例日期时间戳不是,因此我们必须手动定义一个模式
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
您的示例日期时间戳不是,因此我们必须手动定义一个模式
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
很抱歉这是复制粘贴错误。。!你现在能帮我吗?对不起。。。这是复制粘贴错误。。!你现在能帮我吗?