Modify a date object to another format [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
I downloaded date information from an API which is in the format of (Java)
day month monthnumber hour:minute:second EST year
how can I format this into just
month monthnumber year

You could use SimpleDateFormat to first parse the string to a java.util.Date, and then format it to the format you want:
String orig = "Thu Feb 19 19:40:12 EST 2015";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parser.parse(orig);
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
String formatted = formatter.format(date);

Related

Why I get a wrong result when parsing a date from string with SimpleDateFormat ? (Java) [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
How does Java "week year" really work?
(2 answers)
Closed 2 years ago.
I'm trying to parse a date from string but i get a wrong date result and dont understand why :/
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat("MMMM dd, YYYY - HH:mm", Locale.US);
Date date = parser.parse(dateStr);
When I look date result (in debugger) i see : Sun Dec 30 10:48:00 CET 2012
Can someone tell me where I'm wrong please ?
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat(
"MMMM dd, yyyy - hh:mm", Locale.US);
Date date = parser.parse(dateStr);
System.out.println(date);
Use yyyy and not YYYY
Also don't use Date but use LocalDate and LocalDateTime.
Here is how you would do it using LocalDateTime
String dateStr = "September 6, 2013 - 10:48";
DateTimeFormatter format = DateTimeFormatter.ofPattern(
"MMMM d, y - HH:mm",Locale.US);
LocalDateTime date = LocalDateTime.parse(dateStr,format);
System.out.println(date.format(format));
Note the HH is for 24 hour time since you didn't include an AM or PM in your date string.

How to parse this date format "Wednesday, 12 June 2019 14:23:39" to this "2019-03-05T11:56:13Z" in Java? [duplicate]

This question already has answers here:
Java: How to convert string with month name to DateTime?
(1 answer)
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Closed 3 years ago.
How do I parse this date "Wednesday, 12 June 2019 14:23:39" which comes as a String to a date format like this "2019-03-05T11:56:13Z" in JAVA?
You should use the java.time API for this:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse("Wednesday, 12 June 2019 14:23:39", dateTimeFormatter);
String isoDateTime = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(isoDateTime);
which results in:
2019-06-12T14:23:39
attached example of how you can parsar your date
greetings
public static void main(String[] args) throws ParseException {
String date_s = "2011-01-18 00:00:00.0";
// *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dt.parse(date_s);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss");
System.out.println(dt1.format(date));
}
enter image description here

Unable to format timestamp as YYYY-MM-DD HH:mm:ss in java [duplicate]

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I have a timestamp 13 Dec 2018 19:37:18 which I need to convert to 2018-12-13 19:37:18, I am following the below steps but it is giving incorrect timestamp
DateFormat outputFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("DD MMM YYYY HH:mm:ss");
Date date = inputFormat.parse(updatedStartime);
String NewStartTime = outputFormat.format(date);
I am getting the output as 2018-Jan-01 19:37:18, Do I need to convert the MMM to integer month value before formatting the output? what is the correct step to get the expected output?
D is "day in year"; not "day in month" what you actually need, so d.
Y is "week years"; i guess you meant y for normal year
So in all that would be:
DateFormat outputFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
DateFormat inputFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
For more details look at the javadoc https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

how to convert Tuesday , December 25th, 1900 into MM/dd/yyyy format in java [duplicate]

This question already has answers here:
Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string
(5 answers)
How to convert the date value "26th May 2017" to "26/05/2017"? [duplicate]
(4 answers)
Closed 4 years ago.
I have date string like Tuesday , December 25th, 1900, how can I covnert it into MM/dd/yyyy format. I have refereed this link but with a workaround for my date format.
Below is what I tried but I'm getting an exception as Unparsable date.
String caseDate = "Tuesday , December 25th, 1900";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE, MMMM dd Z yyyy");
Date date = inputFormat.parse(caseDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
Utils.logtMsg("formattedDate "+formattedDate);
Exception as
Exception ::: Unparseable date: " Tuesday , December 25th, 1900"
The issue is that your input string does not match your input format. I changed the format and it is working fine. Try,
String caseDate = "Tuesday , December 25th, 1900";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE , MMMM dd'th', yyyy");
Date date = inputFormat.parse(caseDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
System.out.println(formattedDate);

Convert Date to String in JAVA [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Java string to date conversion
(17 answers)
Closed 8 years ago.
I have java.util.Date 02/26/2015. I want to convert it like Feburary 26,2015 as String. How can I convert it like that ??
like what goes here ??
DateFormat formatter = new SimpleDateFormat("here");
Use this format:
DateFormat formatter = new SimpleDateFormat("EEE dd,yyyy");
As descripted in the documentation of SimpleDateFormat:
E Day name in week
d Day in month
y Year
You use SimpleDateFormat
DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy");
The format above is:
MMMM Month in year,
dd Day in month,
yyyy Year,
Please read the manual for more possibilities of how to format dates properly.
SimpleDateFormat format=new SimpleDateFormat("MMMM dd, yyyy");

Categories

Resources