Reputation: 2339
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?
Upvotes: 20
Views: 192421
Reputation: 1
from datetime import datetime
Step 1: Convert string to date
date_str = "2023-11-07"
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
Step 2: Format the date formatted_date = date_obj.strftime("%d-%m-%Y")
print(formatted_date)
Upvotes: -1
Reputation: 137272
Use SimpleDateFormat#format(Date):
String start_dt = "2011-01-01";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse(start_dt);
SimpleDateFormat newFormat = new SimpleDateFormat("MM-dd-yyyy");
String finalString = newFormat.format(date);
Upvotes: 45
Reputation: 95
enter code here
try {
String str_entereddate = "2024-11-08";
DateFormat Ourformatis = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = Ourformatis.parse(str_entereddate);
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
String finalString = outputFormat.format(date2);
Log.e("FinalString", finalString);} catch (Exception ex) {
Log.e("date error", "Error");}
O/P 08-11-2024
If we replace ("yyyy-MM-dd") to ("yyyy-MM-DD");
String str_entereddate = "2024-11-08";
DateFormat Ourformatis = new SimpleDateFormat("yyyy-MM-DD");
Date date2 = Ourformatis.parse(str_entereddate);
SimpleDateFormat outputFormat = new SimpleDateFormat("DD-MM-yyyy");
String finalString = outputFormat.format(date2);
Log.e("FinalString", finalString);
Out Put will be, 08-01-2024
month will appear 01 instead of 11, because
Upvotes: 0
Reputation: 1489
String myFormat= "yyyy-MM-dd";
String finalString = "";
try {
DateFormat formatter = new SimpleDateFormat("yyyy MMM dd");
Date date = formatter.parse("2015 Oct 09");
SimpleDateFormat newFormat = new SimpleDateFormat(myFormat);
finalString= newFormat .format(date );
newDate.setText(finalString);
} catch (Exception e) {
}
Upvotes: 1
Reputation: 331
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();
}
}
Upvotes: 0
Reputation: 12757
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
Upvotes: 0
Reputation: 338181
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.
Upvotes: 1
Reputation: 551
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"));
Upvotes: 0
Reputation: 51030
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
Upvotes: 2
Reputation: 2637
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
Upvotes: 1