how can i convert int to date in java? [duplicate] - java

This question already has answers here:
Good way to convert integer YYYYMMDD into java.util.Date with local time zone
(4 answers)
How can I convert an Integer (e.g 19000101 ) to java.util.Date?
(5 answers)
Type mismatch: cannot convert from java.util.Date to java.sql.Date
(3 answers)
Closed 2 years ago.
I want to convert an int value to a date.
For example, 20200605 is 2020-06-05
This is my code, Why doesn't it work?
public static void main(String[] args) {
int value = 19000101;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(Integer.toString(value));
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = newFormat.format(date);
}
I am getting an error on this line.
Date date = originalFormat.parse(Integer.toString(value));
The error message is
Type mismatch: cannot convert from java.util.Date to java.sql.Date
I don't know what it means.

The error you get:
Type mismatch: cannot convert from java.util.Date to java.sql.Date
tells that you have imported a wrong Date object. Please check at the top of the class. You will need to replace import java.sql.Date; with import java.util.Date;

I don't know much about the Date and SimpleDateFormat classes in java, but this should be fairly easy to do anyway without it.
String date_str = ""+value;
String formattedDate = date_str.substring(0,4)+"-"+date_str.substring(4,6)+"-"+date_str.substring(6,8);

Related

How to convert "2019-04-24" to "24-Apr-2019" date format? [duplicate]

This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Closed 1 year ago.
I am trying to convert "2019-04-24" to "24-Apr-2019" this but it is converting it as "24-Jan-19".
This is the code I am using:
public static String dateFormatConvert(String date, String srcDateFormat, String targetDataFormat) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(srcDateFormat);
Date srcDate = dateFormat.parse(date);
DateFormat destDateFormat = new SimpleDateFormat(targetDataFormat);
return destDateFormat.format(srcDate);
}
Calling it:
String date1 = CommonUtil.dateFormatConvert("2019-04-24", "yyyy-MM-DD", "DD-MMM-YY"); -> This is one of the format used (DD-MMM-YY) out of many
System.out.println(date1);
What's going wrong here?
According to the Documentation.
D - Day in year
d - Day in month
The correct code would be:
date1 = dateFormatConvert("2019-04-24", "yyyy-MM-dd", "dd-MMM-yyyy");

Convert string yyyy-mm-dd to date in javaEE [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Calculating days between two dates with Java
(16 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 2 years ago.
I need to convert the following format of date yyyy-mm-dd to date in format dd/mm/yyyy or dd-mm-yyyy, is it posible. I'm getting unparsable exception.
I have the following code:
String fechaInicial= docudetalle.getfechainicial();
String fechaFinal= docudetalle.getfechafinal();
String fechaEspecial= docudetalle.getfechaespecial();
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date dateInicial = formatter.parse(fechaInicial);
Date dateFinal = formatter.parse(fechaFinal);
Date dateEspecial = formatter.parse(fechaEspecial);
To which once converted I need to get the difference between the three dates, to show in a table as the amount days passed. How can I get the difference in the dates, I know with calendar instance is much easier to get the difference. Is there is an easier method for this?
Use LocalDate and DateTimeFormatter to manipulate dates.
String source = "2020-04-22";
LocalDate src = LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String dst = src.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(dst);
// or
dst = src.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println(dst);
prints
22/04/2020
22-04-2020

Convert String to Time Java.sql.time error [duplicate]

This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 5 years ago.
I want to convert a string to time using the parse method which is going to be inserted to database later. But I get: Incompatible Types: Java.util.date cannot be converted to Java.sql.Date. Any Solution?
String s = time.getText();
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date d = sdf.parse(s);
SimpleDateFormat parse() method returns java.util.Date whereas you have imported java.sql.Date, so change the imported class to java.util.Date
the errors occurs when java.sql.Date imported which is derived from java.util.Date, and DateFormat.parse return a java.util.Date. so you can't assign java.util.Date to derived type java.sql.Date.
import java.util.Date;
//not
import java.sql.Date;
OR assign it to a full-name type of java.util.Date when you both using java.sql.Date and java.util.Date:
java.util.Date d = sdf.parse(s);
OR if you need convert java.util.Date to a java.sql.Date you can do as follows:
java.sql.Date d = new java.sql.Date(sdf.parse(s).getTime());
OR if you need convert java.util.Date to a java.sql.Time you can do as follows:
java.sql.Time d = new java.sql.Time(sdf.parse(s).getTime());

Best way to transform date string into an date object [duplicate]

This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Java string to date conversion
(17 answers)
Closed 6 years ago.
What is the best way to transform like October-2016 into an Date Object?
When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
it throws an exception, because it cannot convert this type of date.
since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat
Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
String st = "October-2016";
DateFormat format = new SimpleDateFormat("MMMM-yyyy");
Date date = format.parse(st);
System.out.println(date);
Both working same:
Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
System.out.println(date);
System.out.println(date1);

Convert java.util.Date to java.sql.Date with 'yyyy-mm-dd hh mm ss' [duplicate]

This question already has answers here:
java sql date time
(2 answers)
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 7 years ago.
I need to pass json string to one of the rest web service. There is a property called 'amortizationdate' which will have format 'yyyy-mm-dd hh mm ss'.
for e.g. {amortizationdate:2015-07-31 00:00:00}
we are having AmortizationVO.java with a property amortizationdate.
for e.g
private java.sql.Date amortizationDate.
public void setAmortizationDate(java.sql.Date date){
}
We need to set the date by calling setAmortizationDate(..date) method and using Jackson to convert AmortizationVO.java to Json.
but in JSON I m getting {amortizationdate:2015-07-31}. But expected result should be with timestamp.(amortizationdate:2015-07-31 00:00:00)
note: I don't want to use util date in my Value Object.
Pls help.
What I've tried:
java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());
ExcelEntityAddressVO entityAddressVO = new ExcelEntityAddressVO();
entityAddressVO.setAmortizationDate(new java.sql.Date(sq.getTime()));
This is my JSON:
{
"amortizationdate" : "2015-07-31",
}
You don't want java.sql.Date - it doesn't keep time, use java.sql.Timestamp
You can try this:
import java.sql.Timestamp;
public class Test {
public static void main(String[] args) {
java.util.Date utilDate = new java.util.Date();
System.out.println("utilDate:" + utilDate);
Timestamp ts = new Timestamp(utilDate.getTime());
System.out.println(ts);
}
}

Categories