Convert a String format to [duplicate] - java

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I want to create a parser program. Every two characters in string will cut into a unit of time, for example: "150901184610801" need to be converted to displayed as "Tue Sep 01 18:46 CST 2015".
I know my question is somewhat complex, so I would like to be divided into different blocks sequentially solve the problem.
The first problem is:
How to use JAVA to format string follow a rule : each two word element cut into a variable, so I can manipulate variables such as yy (15), mm (09), dd (01), hh (18), ss (46).
Thanks again for any suggest.

Actually seems quite simple:
String s = "150901184610801";
SimpleDateFormat df = new SimpleDateFormat("yyMMddhhmmssS");
System.out.println(df.parse(s));
OUTPUT:
Tue Sep 01 18:46:10 CEST 2015
Check SimpleDateFormat API

You can use a SimpleDateFormat

Related

How to convert an existing date object to 24 hrs time format in java? [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
How to convert a String to Date of format yyyy-MM-dd HH:MM:ss [duplicate]
(4 answers)
Sort objects in ArrayList by date?
(14 answers)
Closed 4 years ago.
I have a date object which, if I print, gives me Mon Feb 11 01:21:00 CST 2019. I want to convert this to a 24 HRS date Format. I do not want strings as output or input. Please try to keep the input and output variables as Date object only.
Edit : I want to further use the converted date To use Collections.sort on an Arraylist. I am able to do so in the present implementation as well, but In here 12:55 AM is being ordered way later than 1:15 AM for the same date, which should otherwise be ordered before it.
I am using Java 6. and am overriding compare method from the comparator to do the two parameters based sorting of which date is the second parameter.
Edit 2 : It is different because I want the output as a date Object and not as a string Object. Converting it to a string, using SimpleDateFormat, there are 'n' number of ways to do it but I want the output as a Date object so as to do the sorting and as I mentioned I am running on java 6.
The format of the java.util.Date.toString() method cannot be changed. The Javadoc states:
public String toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
However, this is already in 24-hour format, as the Javadoc states a little bit further down:
hh is the hour of the day (00 through 23), as two decimal digits.

java SimpleDateFormat giving wrong result [duplicate]

This question already has answers here:
Java SimpleDateFormat returns unexpected result
(3 answers)
Closed 6 years ago.
I am using java.text.SimpleDateFormat in Scala to convert string to date.
val isdf = new SimpleDateFormat("dd/MM/yyyy")
isdf.parse("01/22/2016") gives Sun Oct 01 00:00:00 UTC 2017
How to fix this ? Are there any alternative?
Your problem is that you have "22" in the month position. The date format you set is day/month/year, but it looks like the date you sent is in the format month/day/year.
The formatting is different between the dates. You have mixed the month and Day up in your statements. try this:
val isdf = new SimpleDateFormat("MM/dd/yyyy");
goodluck with the code.

Java SimpleDateFormat not parsing correctly [duplicate]

This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 8 years ago.
I'm processing a list of dates from an input file, and I need to convert each from String to Date. Examples of the format:
9/2/2013 7:34:17 PM
1/13/2011 10:47:36 AM
Each time a line is read, the date is stored in the String variable dateAsString. Here's what I've got:
DateFormat format = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
Date myDate = format.parse(dateAsString);
System.out.println(myDate.toString());
The output is incorrect:
9/2/2013 7:34:17 PM becomes Sun Dec 30 19:34:17 EST 2012
1/13/2011 10:47:36 AM becomes Sun Dec 26 10:47:36 EST 2010
It seems pretty straightforward, so I'm confused. What am I doing wrong?
Just try 'yyyy' instead of 'YYYY'
Capital 'Y' means a "week year" where 'y' represents the actual year. Try using yyyy instead.
For more formatting help, check out the SimpleDateFormat API where they give you examples of the patterns to use.
Extra Info
Just in case you're wondering, a "week year" is a year where all the weeks in the year are whole weeks
Your format is not correct, as you can see in Javadoc :
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use yyyy instead of YYYY for the year part

Handling non understandable behaviour of Java date formats. [duplicate]

This question already has answers here:
How to convert a date String to a Date or Calendar object?
(5 answers)
Closed 6 years ago.
As I am not expert in handling of dates in java but I am unable to understand this behaviour.Here is my code
Date from = new SimpleDateFormat("dd/MM/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output
Sat Jul 05 00:07:00 PKT 2013
And this is 2nd another code snippet
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output:
Sat Jan 05 00:07:00 PKT 2013
Now the thing which is considerable is format. This format dd/MM/yyyy which have MM gave me correct output but this format dd/mm/yyyy which have small mm gave me wrong output (always give jan in month).I read the doc where it is mentioned that samll m is for minutes and capital M is for month My question is Can I never use small m here? if no , then why it is giving the result and on which basis it is giving jan everytime I know this is a basic question but after searching and after not finding any understandable thing , I posted it.Thanks
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
that mm in your format is for minutes. MM is for month.
Those formatting placeholders are fixed. small m is always for minutes. And it's January because this is the default Month value.
mm is for minutes so you do not have any month in your date. Thus, I guess that the month is initialized to 0 (Jan)
The reason it does not fail is because by default the formatter is lenient. If you want it to fail then setLenient(false) on the formatter object.
Although I do not think it will fail in your case as in your example it will read 07 as minutes.
When using
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
the simple mm is for minutes therefore i think the value for month is assumed to be 0 so it gives Jan as the default value so use MM for month.
I guess it comes from calendar.clear() which will point to 1 Jan 1970. Then it adds you parsed data = 2013 years (YY), 5 days (dd) and 7 minutes (mm). Use MM for month

get date difference in java, use of getDate() [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
getting the difference between date in days in java [duplicate]
(3 answers)
Closed 10 years ago.
I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?
/* Current format Sat Feb 23 00:00:00 GMT 2013 */
Date date_from = pickerFrom.getDate();
Date date_to = pickerTo.getDate();
int date_diff = (int)((date_to)-(date_from));
Checkout getting the difference between date in days in java
My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation
You can get the difference in milliseconds of each date and subtract these values.
long diff = date_to.getTime() - date_from.getTime();
will return you the number of milliseconds between the two dates.
Then, you can use something like this to get the number of hours, days,... out of these milliseconds.

Categories