Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SimpleDataFormat为不同的本地人设置日期格式_Java_Date_Java.util.date - Fatal编程技术网

Java 使用SimpleDataFormat为不同的本地人设置日期格式

Java 使用SimpleDataFormat为不同的本地人设置日期格式,java,date,java.util.date,Java,Date,Java.util.date,我看到这个问题被问了好几个地方,但给出的答案我不清楚。这就是为什么我要再问一次 是否可以通过使用相同的日期模式传递Locale参数来获取特定于区域设置的日期?例如,我怎样才能做这样的事情 String pattern = "dd/MM/yyyy"; Date d1 = new Date(); SimpleDateFormat f1 = new SimpleDateFormat( pattern , Locale.UK ); f1.format(d1); // formatted date

我看到这个问题被问了好几个地方,但给出的答案我不清楚。这就是为什么我要再问一次

是否可以通过使用相同的日期模式传递Locale参数来获取特定于区域设置的日期?例如,我怎样才能做这样的事情

String pattern = "dd/MM/yyyy";
Date d1 = new Date();
 SimpleDateFormat f1 =  new SimpleDateFormat( pattern , Locale.UK );
 f1.format(d1);
 // formatted date should be in  "dd/MM/yyyy" format

 SimpleDateFormat f2 =  new SimpleDateFormat( pattern , Locale.US );
 f2.format(d1);
 // formatted date should be in "MM/dd/yyyy" format
@Test
public void formatDate() {
    Date today = new Date();
    SimpleDateFormat fourDigitsYearOnlyFormat = new SimpleDateFormat("yyyy");
    FieldPosition yearPosition = new FieldPosition(DateFormat.YEAR_FIELD);

    DateFormat dateInstanceUK = DateFormat.getDateInstance(DateFormat.SHORT, 
            Locale.UK);
    StringBuffer sbUK = new StringBuffer();

    dateInstanceUK.format(today, sbUK, yearPosition);

    sbUK.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), 
            fourDigitsYearOnlyFormat.format(today));
    System.out.println(sbUK.toString());

    DateFormat dateInstanceUS = DateFormat.getDateInstance(DateFormat.SHORT,
            Locale.US);
    StringBuffer sbUS = new StringBuffer();
    dateInstanceUS.format(today, sbUS, yearPosition);
    sbUS.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), 
            fourDigitsYearOnlyFormat.format(today));
    System.out.println(sbUS.toString());
}
上面的代码没有给出预期的结果。有没有办法做到这一点

我尝试过使用DateFormat工厂方法。问题是我无法传递格式模式。它有一些预定义的日期格式(短、中等)

提前感谢…

尝试使用以下方法:

DateFormat.getDateInstance(int style, Locale locale)
SimpleDataFormat的java文档说明:

使用给定的模式和默认值构造 指定区域设置的日期格式符号。注:此构造函数可能 不支持所有区域设置。要实现完全覆盖,请使用中的factory方法 DateFormat类


你可以试试这样的

String pattern = "dd/MM/yyyy";
Date d1 = new Date();
 SimpleDateFormat f1 =  new SimpleDateFormat( pattern , Locale.UK );
 f1.format(d1);
 // formatted date should be in  "dd/MM/yyyy" format

 SimpleDateFormat f2 =  new SimpleDateFormat( pattern , Locale.US );
 f2.format(d1);
 // formatted date should be in "MM/dd/yyyy" format
@Test
public void formatDate() {
    Date today = new Date();
    SimpleDateFormat fourDigitsYearOnlyFormat = new SimpleDateFormat("yyyy");
    FieldPosition yearPosition = new FieldPosition(DateFormat.YEAR_FIELD);

    DateFormat dateInstanceUK = DateFormat.getDateInstance(DateFormat.SHORT, 
            Locale.UK);
    StringBuffer sbUK = new StringBuffer();

    dateInstanceUK.format(today, sbUK, yearPosition);

    sbUK.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), 
            fourDigitsYearOnlyFormat.format(today));
    System.out.println(sbUK.toString());

    DateFormat dateInstanceUS = DateFormat.getDateInstance(DateFormat.SHORT,
            Locale.US);
    StringBuffer sbUS = new StringBuffer();
    dateInstanceUS.format(today, sbUS, yearPosition);
    sbUS.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), 
            fourDigitsYearOnlyFormat.format(today));
    System.out.println(sbUS.toString());
}
它基本上使用样式格式化日期,并使用对象捕捉年份的位置。之后,用四位数字替换年份

输出为:

13/11/2013
11/13/2013
13-11-13
2013-11-13
编辑

与任何图案一起使用

StringBuffer sb = new StringBuffer();
DateFormat dateInstance = new SimpleDateFormat("yy-MM-dd");
System.out.println(dateInstance.format(today));
dateInstance.format(today, sb, yearPosition);
sb.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), 
        fourDigitsYearOnlyFormat.format(today));
System.out.println(sb.toString());
输出为:

13/11/2013
11/13/2013
13-11-13
2013-11-13

无法使用DateFormat,因为只有4种预定义样式可用(短、中等)。我需要定义自己的样式。非常感谢您的回答。但如果日期模式不等于“dd/MM/yyyy”,则会出现问题。是吗?@Dilantha,使用这种方法,你不必传递任何模式,它是通过
locale
style
参数给出的,你只需重放2位数的年份,即4位数的年份。为此,FieldPosition与参数
YEAR\u FIELD
一起使用,以定位年份部分,无论格式如何。@Dilantha,我已编辑了我的答案,以便与任何其他模式一起使用。