2015-02-06 113 views

回答

1

显示日期,您可以使用

private static String tranPattern(String ori) throws ParseException { 
    SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd"); 
    SimpleDateFormat ouputFormat = new SimpleDateFormat("dd-mm-yyyy"); 
    return ouputFormat.format(format.parse(ori)); 
} 
0

一种可能性是分流/退/加入的-

package so28360178; 

import com.google.common.base.Joiner; 
import com.google.common.base.Splitter; 

import static com.google.common.collect.Lists.newArrayList; 
import static com.google.common.collect.Lists.reverse; 
import static java.lang.String.format; 

public class App { 

    public static final Joiner JOINER = Joiner.on('-'); 
    public static final Splitter SPLITTER = Splitter.on('-'); 

    public static void main(final String[] args) { 
     // with guava Splitter/Joiner and Lists#reverse 
     System.out.println(JOINER.join(reverse(newArrayList(SPLITTER.split("2015-02-12"))))); // 12-02-2015 

     // plain old java using String#format for the reverse 
     System.out.println(format("%3$s-%2$s-%1$s", "2015-02-12".split("-"))); // 12-02-2015 
    } 
} 
3

您可以使用SimpleDateFormat,并将给定日期解析为所需的格式。

public static void main(String[] args) throws ParseException { 
     SimpleDateFormat givenFormat = new SimpleDateFormat("yyyy-mm-dd"); 
     Date givendate = givenFormat.parse("2015-02-12"); 
     SimpleDateFormat ouputFormat = new SimpleDateFormat("dd-mm-yyyy"); 
     String newDate = ouputFormat.format(givendate); 
     System.out.println(newDate); 
    } 

输出

12-02-2015 
1

如果输入字符串的格式是固定的,你可以使用的replaceAll方法。

String in = "2015-02-12"; 
//        +-- match four characters in group 1 
//        |  |+-- - match two characters in group 2 
//       vvvv vv vv-- - match two characters in group 3 
String out = in.replaceAll("(....)-(..)-(..)", "$3-$2-$1"); 
//            replace the input string by the 
//            matching groups 
System.out.println("out = " + out); 

如果使用了JavaFX的DatePicker你可以在这里http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/date-picker.htm看看。

相关问题