Converting date string build from date and time picker [duplicate] - java

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 1 year ago.
I have a date and time picker and I build a string date using these two. Here's a sample date and time string.
"11/6/2013 09:23"
Now I need to convert them into a date and convert them to this format "yyyy-MM-dd'T'HH:mm:ss.SSS".
My problem is I'm having this error in my logcat:
11-06 21:23:53.060: E/Error(26255): Unparseable date: "11/6/2013 09:23" (at offset 2)
I'm using this code to do the conversion of string to date, and the date to a formatted string.
Date d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = d.toString();
When I use new Date and get the current date, it works fine. I guess the format of my string is wrong. But I'm displaying it on an edittext in that format. I want to stay it in that way. Is there anyway to convert this string format to a date? Any ideas guys? Thanks!

I think you are trying to do 2 things at once. In order to convert any String to another String in a different format you need to:
Parse the string with a DateFormat containing the current format, this returns a Date; then
take your newly obtained Date and format it under the desired DateFormat
SimpleDateFormat formatOne = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date date = formatOne.parse(etaToDeliverArrivedAtShipper.getText().toString());
SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.getDefault());
String result = formatTwo.format(date)

you are trying to parse in yyyy-MM-dd'T'HH:mm:ss.SSS but it is not. it is probably yyyy/M/dd HH:mm:ss
What you probably want is:
Date d = new SimpleDateFormat("yyyy/M/dd HH:mm:ss", Locale.getDefault()).parse(etaToDeliverArrivedAtShipper.getText().toString());
ETAtoNextStop = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.getDefault()).format(d);

This is simply because your pattern is not matching the date string you are trying to parse. There are 3 errors in your pattern :
You use MM which means the month should be on two digits, while in the date it is only 1 digit
You use .SSS which means there are milliseconds but your date is not that precise
You are using the wrong delimiter
So the right pattern should be : yyyy/M/dd HH:mm:ss
To get the desired format, then create a new SimpleDateFormat object with the desired pattern and use the parse(Date) method giving it the Date object previously returned by parse().

Related

Reformat custom date from string [duplicate]

This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 2 years ago.
I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:
String date = "2020-09-05";
Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:
Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);
For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".
Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?
You need to make a distinction between parsing the date (turning a String into a Date or LocalDate class) and formatting a date (turning the Date or LocalDate class instance to a String).
To parse the initial string, use:
LocalDate convertedDate = LocalDate.parse(date)
To format the convertedDate, use:
String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"))
EDIT: I am assuming you are at least on Java 8, so you can use the newer date/time classes. See https://www.baeldung.com/java-8-date-time-intro for some more info.
You are mixing up parsing and formatting: your code is trying to parse a string given the format. You’re telling Java you’re expecting the date to contain the week name.
You need to first parse your date in the format it’s in. This will give you a date. You next need format your date with the desired format, which will give you a string:
final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);
ok here's the code:
String sDate1="1998-12-31";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sDate1);
System.out.println("EEE, d MMM yyyy formatted date : " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));

JSON date to formatted JAVA date [duplicate]

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

Convert Date input and Time input into a single date object java [duplicate]

This question already has answers here:
Combining java.util.Dates to create a date-time
(3 answers)
Closed 7 years ago.
I'm using datepair.js which can be found here http://jonthornton.github.io/Datepair.js/
I'm building a leave request calendar where the end user can select a particular date in one input field and a time in a separate input field. I'm looking for the best way to parse these two fields into a single date/time field for the db.
I have the following JSON
[startTime:12:30am, startDate:11/12/2015, endDate:11/12/2015]
and the following SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
I know I can set the date by doing something like
new Date(sdf.parse(jsonObj.startDate))
But what is the best way to combine those dates?
Ok, this is a bit of a kludge, but have you considered retrieving startDate and startTime as strings, concatenating them with a space (or something) in the middle, and then using a single SimpleDateFormat to parse the combined string? Something like this:
String datetime = jsonObj.startDate + " " + jsonObj.startTime;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mma"); //changed the format to your example input
Date d = sdf.parse(datetime);

Get Date Object with formatted date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-
Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);
dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.
I want something similar which will return formatted date in Date data type.
That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.
Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.
Given your example code, it would look like this:
String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);

Getting formatted date in date format rather than String

I'm running the program written below, but instead of printing in mm/dd/yyyy hh:mm format it prints in the normal date format(ie. Day Date and time)
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy hh:mm");
Date date = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
The reason i'm doing this is because the existing method accepts parameters in Date format, so i need to send the above mentioned date object to it.
Please point out the mistake or suggest some other alternative.
Thanks
Date objects don't have a format. The Date class is a wrapper around a single long, the number of milliseconds since the epoch. You can't "format" a Date, only a String. Pass around a Date/Calendar internally, and format it whenever you need to display it, log it, or otherwise return it to the user.
Change the format to MM/dd/yyyy. Month is denoted by capital M.
Check below URL for valid formats
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your formatter works quite fine (apart from the mm vs. MM bug). You get a formatted string from the date and then create a copy from your date by parsing the formatted string:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
Date now = Calendar.getInstance().getTime();
String formattedNow = sdf.format(now); // == "09/24/2013 01:59"
Date now2 = sdf.parse(formattedNow); // == now

Categories

Resources