Convert String to Date [duplicate] - java

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?

First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);

The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));

Related

Converting Java string to Date [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 4 years ago.
I need to convert a java string in a date format to a new date format to send it to my sql-server. Currently, my Java date is Jul 17 2018 14:22:58, and I need it to look like 2018-07-17 14:22:58. So I created this:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
Date formattedDate = sdf.parse(oldTime);
I am getting errors that my oldTime is unparseable. Did I do this wrong?
You need to use a format string that matches your date string:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");

Get current date in YYYY-MM-DD format in Java (Android) [duplicate]

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

How can I extract date from since time component [duplicate]

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 7 years ago.
I have to extract date in the format MM-dd-yyyy in java from the since time value. Since time is the time at which the doucment is created. For example, if since time is 1452413972759, date would be "Sun, 10 Jan 2016 08:19:32 GMT" (Calculated from http://www.epochconverter.com/) . From this, I could get date in desired format but I am unable to code for the first step i.e., converting since time to date. Can someone help me?
I tried
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date date = df.parse("1452320105343");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
But it gives parse exception.
Your code can't work because you're trying to parse a number of milliseconds as if it was a MM/dd/yyyy formatted date.
I would have expected the following code to work, where S represents milliseconds :
DateFormat df = new SimpleDateFormat("S");
java.util.Date date = df.parse("1452413972759");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
However it doesn't for some reason, displaying a date in the 1970 year.
Instead, the following code that parses seconds rather than milliseconds works for your needs :
DateFormat df = new SimpleDateFormat("s");
java.util.Date date = df.parse("1452413972"); // just remove the last 3 digits, or divide by 1000
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
Or just follow the link from #mohammedkhan and use Date constructor rather than parsing a string :
java.util.Date date = new Date(1452413972759l);
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));

java pick up CEST/CET from string [duplicate]

This question already has an answer here:
The date format issue from java
(1 answer)
Closed 9 years ago.
I have strings like:
02/03/07 11:13:33 CEST or
02/03/07 11:13:33 CET.
How do I parse it to a util.Date in such a way that it considers cet/cest?
You need to use SimpleDateFormat object to do it:
Date d=new SimpleDateFormat("dd/MM/yy HH:mm:ss z").parse(string);
Here z stands for the zone.
Use a SimpleDateFormat with the right format string:
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss z");
String text1 = "02/03/07 11:13:33 CEST";
String text2 = "02/03/07 11:13:33 CET";
Date d1 = df.parse(text1);
Date d2 = df.parse(text2);

How to convert java.lang.string To java.util.date in java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to parse date in java?
I am getting "Mon Aug 01 00:00:00 IST 2011" (java.lang.String) as string and i need to convert it to "01-08-2011" Date(java.Util.Date) ,how to do this
use the SimpleDateFormat class
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
to print the date. This will print the current date in predefined formatted using SimpleDateFormat
System.out.println("Current Date " + sdf.format(new Date());
String mydate = "01-08-2011";
to convert string into date
Date parseDate = sdf.parse(mydate);
Please take a look at (Simple)DateFormat's parse method.
Look at the SimpleDateFormat class in the javadocs.
Constructs a SimpleDateFormat using the given pattern and the default
date format symbols for the default locale
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date " + sdf.format(new Date());

Categories

Resources