This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I am working on a Java application and I have the following problem trying to format a date.
So I inizialize an object field with a new java.util.Date; that represent the current date time, this one:
progetto.setDatOraUltMov(new Date());
When I print this field the result is somethind like this:
Mon Oct 12 17:19:06 CEST 2015
Ok, this standard is not good for my pourpose and I want that is shown something like this:
12/10/2015 17:19:06
Something like in the format DAY/MONTH/YEAR HOUR:MINUTE:SECOND
How can I do something like this? How can I specify the required date format?
Use SimpleDateFormat.
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
DateToStr = format.format(curDate);
For your reference
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You can try this
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-YYYY-hh:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
System.out.println(dateNow);
import
import java.text.SimpleDateFormat;
import java.util.Calendar;
Related
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
This question already has answers here:
How to set the TimeZone for String parsing in Android
(3 answers)
Closed 6 years ago.
I get from the server is like 2017-01-24T16:16:30.690Z.
This date is in GMT time zone.
I want to convert this time into GMT+6 time zone as well as time format.
My expected result is: 24 January 2017 22:16
See above comments. If you apply those, try:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2017-01-24T16:16:30.690Z", new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+06:00"));
sdf.applyPattern("d MMMM yyyy HH:mm");
String formatted = sdf.format(date);
Worked for me.
This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 6 years ago.
I receive DATETIME from mysql database in a format like this 2016-10-12 18:52:00. Is there an easy way to print it in a readable like 12 October 2016 6:52 pm in java or android?
You can do things like:
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/DD/YYYY HH:mm:ss");
Date date = new Date();
String S = simpleDate.format(date); //This will give you like 11/20/2016 08:45:02
Now use this form and get the month name programatically according to your need.
This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
Update:
Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????
I'm trying to create java Date but it's always return wrong value. This is my code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");
And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value.
Please don't suggest to use external library for date type.
Update:
I changed my pattern to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.
Note that:
MM are the months, mm are the minutes.
SS are the milliseconds, ss are the seconds.
So you need to change your dateFormat to
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Basically there are two errors in your pattern, both in the time part (seconds and minutes).
Here is the link to the complete documentation link.
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 8 years ago.
I am trying from half an hour to convert string to date by using following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date lastCharged = dateFormat.parse(lastChargeDate);
Every time I run this code the date returned by the system is Sun Dec 29 00:00:00 PKT 2013 Even if i changed the date manually same is the response by the system.
Any help in this regard a lot of work is suspended just because of this blunder.
DateFormat#parse() method just convert the String to Date. It doesn't change anything in the converted Date it means it doesn't store the format from which it is constructed.
Whenever you print the Date object again then it prints in its default toString() implementation that's what you are getting.
It you need to print it again in specific format then use DateFormat#format() method.
The format should be yyyy-MM-dd instead of YYYY-MM-dd.
Sample code:
String oldDate="2014-06-07";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date=dateFormat.parse(oldDate);
System.out.println(date);
String newDate=dateFormat.format(date);
System.out.println(newDate);
output:
Sat Jun 07 00:00:00 IST 2014
2014-06-07