Kann mir jemand sagen, welche Datumsformate in der SimpleDateFormat-Klasse verfügbar sind?
Ich habe durch api gegangen, konnte aber nicht finden, eine zufriedenstellende Antwort.jede Hilfe ist sehr geschätzt.
Die Datums- und Zeitformate werden im Folgenden genau beschrieben
SimpleDateFormat (Java Platform SE 7) - Date and Time Patterns
Es gibt n
Anzahl von Formaten, die Sie möglicherweise machen können. z.B. - dd/MM/yyyy
oder YYYY-'W'ww-u
oder Sie können die Buchstaben mischen und anpassen, um Ihr gewünschtes Muster zu erreichen. Die Buchstaben des Musters sind wie folgt.
Zum Parsen:
2000-01-23T04:56:07.000+0000
Verwenden:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Ich gebe Ihnen ein paar Beispielcodes, die ich von http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html habe. Dann können Sie mit verschiedenen Optionen herumspielen, bis Sie es verstehen.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date now = new Date();
//This is just Date's toString method and doesn't involve SimpleDateFormat
System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy
//Shows "Mon Oct 08 08:17:06 EDT 2012"
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
System.out.println("Format 1: " + dateFormatter.format(now));
// Shows "Mon, 2012-10-8 at 8:17:6 AM EDT"
dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Format 2: " + dateFormatter.format(now));
// Shows "Mon 2012.10.08 at 08:17:06 AM EDT"
dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
System.out.println("Format 3: " + dateFormatter.format(now));
// Shows "Monday, October 8, 2012"
// SimpleDateFormat can be used to control the date/time display format:
// E (day of week): 3E or fewer (in text xxx), >3E (in full text)
// M (month): M (in number), MM (in number with leading zero)
// 3M: (in text xxx), >3M: (in full text full)
// h (hour): h, hh (with leading zero)
// m (minute)
// s (second)
// a (AM/PM)
// H (hour in 0 to 23)
// z (time zone)
// (there may be more listed under the API - I didn't check)
}
}
Viel Glück!
Prüfen Sie die Formate hier http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Haupt
System.out.println("date : " + new classname().getMyDate("2014-01-09 14:06", "dd-MMM-yyyy E hh:mm a z", "yyyy-MM-dd HH:mm"));
Methode
public String getMyDate(String myDate, String returnFormat, String myFormat)
{
DateFormat dateFormat = new SimpleDateFormat(returnFormat);
Date date=null;
String returnValue="";
try {
date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
returnValue = dateFormat.format(date);
} catch (ParseException e) {
returnValue= myDate;
System.out.println("failed");
e.printStackTrace();
}
return returnValue;
}