How to format java.sql.date in this format: "MM-dd-yyyy"?

I need to get java.sql.date in the following format "MM-dd-yyyy", but I need it to remain java.sql.date, so I can put it in the table as a date field. Thus, after formatting, it cannot be a string, it should be like a java.sql.date object.

This is what I have tried so far:

java.util.Date 
today=new Date();
String date = formatter.format(today); 
Date todaydate = formatter.parse(date);
java.sql.Date fromdate = new java.sql.Date(todaydate.getTime());
java.sql.Date todate=new java.sql.Date(todaydate.getTime()); 
String tempfromdate=formatter.format(fromdate);
String temptodate=formatter.format(todate); 
java.sql.Date fromdate1=(java.sql.Date) formatter.parse(tempfromdate); 
java.sql.Date todate1=(java.sql.Date) formatter.parse(temptodate);
+4
source share
6 answers

You can do it the same way java.util.Date(since it java.sql.Dateis a subclass java.util.Date) withSimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat(
    "MM-dd-yyyy");
int year = 2014;
int month = 10;
int day = 31;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1); // <-- months start
                                    // at 0.
cal.set(Calendar.DAY_OF_MONTH, day);

java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));

Exit - Expected

10-31-2014

+6
source

Use the code below, which I converted today. learn from it and try with code

          Date today = new Date();

        //If you print Date, you will get un formatted output
        System.out.println("Today is : " + today);

        //formatting date in Java using SimpleDateFormat
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
        String date = DATE_FORMAT.format(today);
        System.out.println("Today in MM-dd-yyyy format : " + date);

            Date date1 = formatter.parse(date);
    System.out.println(date1);
    System.out.println(formatter.format(date1));
+2

.

SELECT date_column from YourTable where UNIX_TIMESTAMP(date_column) > ?;

date.getTime() ?.

. UNIX_TIMESTAMP MySQL. .

+1

formatter.parse java.util.Date a java.sql.Date

, java.util.Date, java.sql.Date,

java.sql.Date sqlDate = new java.sql.Date (normalDate.getTime ());

, , , .

0
java.util.Date today=new Date();
java.sql.Date date=new java.sql.Date(today.getTime()); //your SQL date object
SimpleDateFormat simpDate = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(simpDate.format(date)); //output String in MM-dd-yyyy

, , mm-dd-yyyy , (java.sql.Date java.util.Date), , . - .

0

, 2017 , LocalDate java.time, API Java, java.sql.Date. .

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-uuuu", Locale.US);
    LocalDate fromDate = LocalDate.now(ZoneId.of("Asia/Kolkata"));
    String tempFromDate = fromDate.format(formatter);
    System.out.println(tempFromDate);

-

11-25-2017

LocalDate, java.sql.Date . , , - , , , .

int . int 4284. 4 284 4 284, 004284 . int. . LocalDate ( JDBC- , , PreparedStatement.setObject()).

Getting today's date is a time-sensitive operation because it does not match the date in all time zones of the world. I highly recommend that you make this fact explicit in your code. In my fragment, I used the Asia / Kolkata time zone, please replace the desired time zone. You can use the ZoneId.systemDefault()JVM to set the time zone, but remember that this parameter can be changed under our feet by other parts of your program or by other programs running in the same JVM, so it is fragile.

0
source

Source: https://habr.com/ru/post/1545319/


All Articles