Je souhaite formater le 2012-05-04 00:00:00.0
au 04-MAY-2012
. Je l’ai essayé avec les étapes ci-dessous.
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd 'T' HH:mm:ss.SSS"); Date date; Ssortingng dateformat = ""; try { date = sdf.parse("2012-05-04 00:00:00.0"); sdf.applyPattern("DD-MON-RR"); dateformat = sdf.format(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }
mais je suis au-dessous de l’exception.
java.text.ParseException: Unparseable date: "2012-05-04 00:00:00.0" at java.text.DateFormat.parse(DateFormat.java:337) at com.am.test.Commit.main(Example.java:33)`
Comment pourrais-je faire ça?
Ici, cela fonctionne:
Jetez un coup d’œil à la Javadoc de SimpleDateFormat
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class temp2 { public static void main(Ssortingng[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date date; Ssortingng dateformat = ""; try { date = sdf.parse("2012-05-04 00:00:00.0"); sdf.applyPattern("dd-MMM-yyyy"); dateformat = sdf.format(date); System.err.println(dateformat); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Je pense que si vous supprimez le 'T'
cela fonctionnera.
En utilisant ce modèle:
sdf.applyPattern("DD-MMM-YYYY");
Ne pas utiliser ceci:
sdf.applyPattern("DD-MON-RR");
public static void main(Ssortingng[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); Date oldFormatedDate = null; try { oldFormatedDate = sdf.parse("2012-05-04 00:00:00.0"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(new SimpleDateFormat("dd-MMM-yyyy"). format(oldFormatedDate)); }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date date; Ssortingng dateformat = ""; try { date = sdf.parse("2012-05-04 00:00:00.0"); sdf.applyPattern("dd-MMM-yyyy"); dateformat = sdf.format(date); System.out.println(dateformat); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }