This question already has answers here:
Android: Compare time in this format `yyyy-mm-dd hh:mm:ss` to the current moment
(5 answers)
Conversion of a date to epoch Java [duplicate]
(4 answers)
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
Closed 2 years ago.
The following code gave me Datetimestamp as [ 2020-07-183 17:07:55.551 ]. The issue is with "Day" in Datetimestamp, which has three digits. How to format currentTimeMillis into the right format for day of month?
public String Datetimesetter(long currentTimeMillis, SimpleDateFormat dateFormat) {
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTimeMillis);
return dateFormat.format(calendar.getTime());
}
SOLUTION WHICH WORKED FOR ME:
Please visit this link.
This is for the case you are supporting Apps from API level 26 (native support of java.time) or you are willing / allowed to use a backport library of the same functionality.
Then you can use a correct / matching pattern (one that considers three-digit days) like this:
public static void main(String[] args) {
// mock / receive the datetime string
String timestamp = "2020-07-183 17:07:55.551";
// create a formatter using a suitable pattern (NOTE the 3 Ds)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-DDD HH:mm:ss.SSS");
// parse the String to a LocalDateTime using the formatter defined before
LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf);
// and print its default String representation
System.out.println(ldt);
}
which outputs
2020-07-01T17:07:55.551
So I guess the day of year no. 183 was actually July 1st.
your date format is incorrect
dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS.SSS");
change to this
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:SS.SSS");
Related
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Java string to date conversion
(17 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 years ago.
I have string with format : '20018-03-03 11:00:00', and i want to convert to Date but keeping this format. Is this possible? Because when I do something like this :
Date.parse(string), I don't get this format, event when I use
SimpleDateFormat. What I'm doing wrong ?
I tried this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.parse(entry.getValue(), formatter);
String formatDateTime = now.format(formatter);
// Date date = df.parse(sqlDate);
Date d = (Date) formatter.parse(formatDateTime);
It might not be the case in other languages, but in Java the format used to parse a date is not stored in the date itself. Thus you have to reuse the same format when you want to print (format) a date.
Old (Date, SimpleDateFormat) and new (LocalDateTime, etc.) API should not be mixed together. Stick to the new one unless you have legacy code to deal with.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Parse: String -> LocalDateTime
LocalDateTime now = LocalDateTime.parse("2018-03-03 11:00:00", formatter);
// Format: LocalDateTime -> String
System.out.println(now.format(formatter));
This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 4 years ago.
I receive this "10/1/2018, 1:27:42 PM" as date from server. Now want to convert it to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). But to do this i need to know the format of existing date string.
How i can find a correct format of "10/1/2018, 1:27:42 PM"?
You need to use SimpleDateFormat.toPattern() to get the pattern. Use this function in this way -
SimpleDateFormat sdf = new SimpleDateFormat();
// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());
// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);
This question already has answers here:
Month issue in SimpleDateFormat class
(6 answers)
Closed 5 years ago.
I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
How might I get the current date in the YYYY-MM-DD format?
Change this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month
This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below
This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I really need your help. I have searched and tried every example I could use, but none have worked.
I need to store current date in YYYY-MM-DD format in a text file..the String date has to be a string..
String dateF = "YYYY-MM-DD";
Date dateOnly = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateF);
String date = dateFormat.format(dateOnly);
when I tried that code above.. this the output I got
please|work|2016-04-110|11
please help me...this is my assignment due this Friday ): I just need this date and 2 other things to be done..
thanks :)
your issue comes from the case you used for Y and D. according to the API SimpleDateFormat documentation, you should use d (day in month) instead of D (day in year), in your format definition.
String dateF = "yyyy-MM-dd";
Format being used is incorrect.
YYYY-MM-DD : Capital DD will return Day in the year. So, 11 April corresponds to 110th day in the year.
yyyy-MM-dd : Small dd will return the Day in the month.
Refer: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this code :
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(c.getTime());
The formattedDate contain 2016-04-19
Edit :
In your code change the YYYY to yyyy and DD to dd.