Change Date print format from yyyy-mm-dd to dd-mm-yyyy
a) Parse
d <- as.Date( "06.12.2012", "%d.%m.%Y")
b) Format
strftime(d, "%m-%d-%Y")
or
format(d, "%m-%d-%Y")
How to change date format from YYYY/MM/DD to DD/MM/YYYY
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
R has two very similarly named functions: strptime
, which converts from character strings to Date data, and strftime
, which converts Dates to formatted strings. To make matters worse, the documentation for these two functions is combined, so it can be very hard to keep their uses straight. You want strftime
, in this case.
I need to change date format from dd-MM-yyyy to yyyy-MM-DD
LocalDate date = LocalDate.parse (userDto.getTime(), DateTimeFormatter.ofPattern("dd-MM-yyyy"));
Hopefully this should work fine if you are passing in this "dd-MM-yyyy" format.
https://help.gooddata.com/cloudconnect/manual/date-and-time-format.html >> this is a good link for Java DateTimeFormatter
patterns.
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
Date
is a container for the number of milliseconds since the Unix epoch ( 00:00:00 UTC on 1 January 1970).
It has no concept of format.
Java 8+
LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(ldt));
System.out.println(ldt);
Outputs...
05-11-2018
2018-05-11
2018-05-11T17:24:42.980
Java 7-
You should be making use of the ThreeTen Backport
Original Answer
For example...
Date myDate = new Date();
System.out.println(myDate);
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(myDate));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(myDate));
System.out.println(myDate);
Outputs...
Wed Aug 28 16:20:39 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 16:20:39 EST 2013
None of the formatting has changed the underlying Date
value. This is the purpose of the DateFormatter
s
Updated with additional example
Just in case the first example didn't make sense...
This example uses two formatters to format the same date. I then use these same formatters to parse the String
values back to Date
s. The resulting parse does not alter the way Date
reports it's value.
Date#toString
is just a dump of it's contents. You can't change this, but you can format the Date
object any way you like
try {
Date myDate = new Date();
System.out.println(myDate);
SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dmyFormat = new SimpleDateFormat("yyyy-MM-dd");
// Format the date to Strings
String mdy = mdyFormat.format(myDate);
String dmy = dmyFormat.format(myDate);
// Results...
System.out.println(mdy);
System.out.println(dmy);
// Parse the Strings back to dates
// Note, the formats don't "stick" with the Date value
System.out.println(mdyFormat.parse(mdy));
System.out.println(dmyFormat.parse(dmy));
} catch (ParseException exp) {
exp.printStackTrace();
}
Which outputs...
Wed Aug 28 16:24:54 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 00:00:00 EST 2013
Wed Aug 28 00:00:00 EST 2013
Also, be careful of the format patterns. Take a closer look at SimpleDateFormat
to make sure you're not using the wrong patterns ;)
Convert yyyy-mm-dd to dd-mm-yyyy in Hive and how to manage this format date?
Hive supports the date datatype, but it needs to be stored in the format yyyy-MM-dd
. So in order for you to store a date datatype you need to convert it to that format.
select to_date(from_unixtime(unix_timestamp('01-01-2099','dd-MM-yyyy')))
Change date format dd-MM-yyyy to yyyy-MM-dd in Java
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
Related Topics
Error Using T.Test() in R - Not Enough 'Y' Observations
Sum Columns Row-Wise with Similar Names
How Is Ggplot2 Plus Operator Defined
Reshape R Data with User Entries in Rows, Collapsing for Each User
Conditionally Remove Leading or Trailing '.' Character in R
Populate Nas in a Vector Using Prior Non-Na Values
Calculate Centroid Within/Inside a Spatialpolygon
Get First Entries in Rows of List
Sum Columns by Group (Row Names) in a Matrix
Combining .Sd with Renamed Variable Messes with Names of .Sd Columns
How to Obtain All Combinations of the Columns of a Data Frame Taken by 2
Understanding Bandwidth Smoothing in Ggplot2
Using Tidy Eval for Multiple Dplyr Filter Conditions
Function for Polynomials of Arbitrary Order (Symbolic Method Preferred)
Shiny Leaflet Easyprint Plugin
Count Number of Distinct Values in a Vector
Add a Series of Elements in Different Locations Within a Vector