I am formatting a string to a date using the code
String start_dt = '2011-01-01';
DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date)formatter.parse(start_dt);
But how do I convert the date from YYYY-MM-DD
format to MM-DD-YYYY
format?
This question is related to
java
date
format
simpledateformat
Currently, i prefer using this methods:
String data = "Date from Register: ";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Verify that OS.Version is > API 26 (OREO)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// Origin format
LocalDate localDate = LocalDate.parse(capitalModels.get(position).getDataServer(), formatter); // Parse String (from server) to LocalDate
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
//Output format
data = "Data de Registro: "+formatter1.format(localDate); // Output
Toast.makeText(holder.itemView.getContext(), data, Toast.LENGTH_LONG).show();
}else{
//Same resolutions, just use legacy methods to oldest android OS versions.
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd",Locale.getDefault());
try {
Date date = format.parse(capitalModels.get(position).getDataServer());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
data = "Date from Register: "+formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
String start_dt = "2011-01-31";
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) parser.parse(start_dt);
DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(formatter.format(date));
Prints: 01-31-2011
LocalDate.parse( "2011-01-01" )
.format( DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) )
The other Answers are now outdated. The troublesome old date-time classes such as java.util.Date
, java.util.Calendar
, and java.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes.
The input string 2011-01-01
happens to comply with the ISO 8601 standard formats for date-time text. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = LocalDate.parse( "2011-01-01" ) ;
Generate a String in the same format by calling toString
.
String output = ld.toString() ;
2011-01-01
DateTimeFormatter
To parse/generate other formats, use a DateTimeFormatter
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" ) ;
String output = ld.format( f ) ;
01-01-2011
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
String myFormat= "yyyy-MM-dd";
String finalString = "";
try {
DateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
Date date = (Date) formatter .parse("2015 Oct 09");
SimpleDateFormat newFormat = new SimpleDateFormat(myFormat);
finalString= newFormat .format(date );
newDate.setText(finalString);
} catch (Exception e) {
}
In one line:
String date=new SimpleDateFormat("MM-dd-yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse("2011-01-01"));
Where:
String date=new SimpleDateFormat("FinalFormat").format(new SimpleDateFormat("InitialFormat").parse("StringDate"));
String start_dt = "2011-01-01"; // Input String
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // Existing Pattern
Date getStartDt = formatter.parse(start_dt); //Returns Date Format according to existing pattern
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy");// New Pattern
String formattedDate = simpleDateFormat.format(getStartDt); // Format given String to new pattern
System.out.println(formattedDate); //outputs: 01-01-2011
Tested this code
java.text.DateFormat formatter = new java.text.SimpleDateFormat("MM-dd-yyyy");
java.util.Date newDate = new java.util.Date();
System.out.println(formatter.format(newDate ));
http://download.oracle.com/javase/1,5.0/docs/api/java/text/SimpleDateFormat.html
Source: Stackoverflow.com