1

I am converting current date into specific format using following line:

new SimpleDateFormat("yyyyMMdd").format(new Date())

This line returns String. How can I convert this string back to date. I want to use the formatted date in SQL query for comparison with date field.

One line java code is expected as I want to make use of it in jasper report in expression field.

Madhusudan
  • 4,637
  • 12
  • 55
  • 86
  • Are you happy using the system default time zone and locale? (That's what you're getting now...) – Jon Skeet Apr 27 '15 at 05:35
  • I tried to use "Date parse = new SimpleDateFormat("yyyyMMdd").parse(new SimpleDateFormat("MMyyyydd").format(new Date()));" but its giving me result in actual format not in my required format. – Madhusudan Apr 27 '15 at 05:35
  • @JonSkeet: No. I want in "yyyyMMdd" format only. – Madhusudan Apr 27 '15 at 05:35
  • 1
    @RushikeshDaad: Um, that's an entirely separate matter. Converting a `Date` to a date (year/month/day) requires the application of a time zone and a calendar. The calendar is less likely to be an issue, but you *definitely* need to think about which time zone you're interested in. Also, you seem to be under the impression that a `Date` value has a format - it doesn't. It's just a number of milliseconds since the Unix epoch. – Jon Skeet Apr 27 '15 at 05:38

3 Answers3

3

use parse method of SimpleDateFormat for that.

new SimpleDateFormat("yyyyMMdd").parse(<string>)

for more info please check official documentation :

https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
  • I tried to use "Date parse = new SimpleDateFormat("yyyyMMdd").parse(new SimpleDateFormat("MMyyyydd").format(new Date()));" but its giving me result in actual format not in my required format. – Madhusudan Apr 27 '15 at 05:36
  • so what is your requirement. Do you want to compare two java.sql dates? – Sachin Gupta Apr 27 '15 at 05:45
  • In my database dates are stored in format "yyyyMMdd". I want to compare this column with my parameter currentDate. Is there any other way? – Madhusudan Apr 27 '15 at 05:49
  • what is the data type of that column. Date or varchar ? – Sachin Gupta Apr 27 '15 at 05:51
  • you can get your column value in String format from Database. then Convert that String to java.util.date format using `new SimpleDateFormat("yyyyMMdd").parse()` . then you can compare those two java.util.dates easily. – Sachin Gupta Apr 27 '15 at 05:55
0

You can use parse(String source) method form SimpleDateFormat which takes string and converts it to date like this:

String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
Date parse = new SimpleDateFormat("yyyyMMdd").parse( format );
Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
0

What do you understand by Formatting ? it's simply the way in which something is arranged or set out. If i say format the document i don't actually change the type of the document i change how it's organised to the way how i want.

While parsing is changing some kind of data into another kind of data.

so here according to your requirement you are changing string to date so first you need to parse and then format it according to your desired format.

One way to do it

 Scanner sc=new Scanner(System.in);
   System.out.println("Enter the date");
   String stringDate=sc.nextLine();
   DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");//give it your desired format 
   Date date=new Date();
    try {
        date=dateFormat.parse(stringDate);
    } catch (ParseException e) {
            e.printStackTrace();
        }

Edit: Since in comment you says.. you want current date in to be formatted in one line try this it will work.

import java.text.SimpleDateFormat;
import java.util.Date;

public class IntermTest {
    public static void main(String...strings ){
        System.out.println((new SimpleDateFormat("yyyyMMdd")).format(new Date()));

    }
}
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44