I have two date fields in my mysql db.
They look like so in my db.
When I pull them into java using resultSet.getDate I get the following dates in java...
29/01/2016 00:00:00,3/01/2016 00:00:00
My mapping code is below...
masterList.setRpmPromoCompDetailStartDate(resultSet.getDate(DaoConstants.COLUMN_HEADER_RPM_PROMO_COMP_DETAIL_START_DATE));
masterList.setRpmPromoCompDetailEndDate(resultSet.getDate(DaoConstants.COLUMN_HEADER_RPM_PROMO_COMP_DETAIL_END_DATE));
Where my domain object looks like this...
public class MasterList {
private Date rpmPromoCompDetailStartDate;
private Date rpmPromoCompDetailEndDate;
Everything in the output looks right except the month. Can anyone give me some advice on what might be wrong?
thanks
EDIT
Here is my attempted fix...
Timestamp startDateTs = resultSet.getTimestamp(DaoConstants.COLUMN_HEADER_RPM_PROMO_COMP_DETAIL_START_DATE);
java.util.Date startDate = null;
if (startDateTs != null){
startDate = new java.util.Date(startDateTs.getTime());
}
masterList.setRpmPromoCompDetailStartDate(startDate);
Timestamp endDateTs = resultSet.getTimestamp(DaoConstants.COLUMN_HEADER_RPM_PROMO_COMP_DETAIL_END_DATE);
java.util.Date endDate = null;
if (endDateTs != null){
endDate = new java.util.Date(endDateTs.getTime());
}
Use resultset.getTimeStamp to get date
java.util.Date date;
Timestamp timestamp = resultSet.getTimestamp(i);
if (timestamp != null)
date = new java.util.Date(timestamp.getTime()));
Related
I have a column called start_date in database and the field has a mapping in model class like
#Column(name = "start_date")
#Temporal(TemporalType.TIMESTAMP)
Date startDate
and in my pojo the same field I have as
String startDate;
from UI I am getting the string value and I want to convert that string to date and store into database
modelObj.setStartDate(parse(pojoObj.getStartDate())
and here is the parse() method
private Date parse(String dateValue){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(dateValue);
return date;
}
this code is working fine when I am providing date in yyyy-MM-dd format but not working in dd-MM-yyyy or yyyy-MMM-dd or MMM dd, yyyy any other format.
can I someone help me how can I persist the date field irrespective it's format.
As String I am able to store but I want to store it as date.
I give you a solution, the database uses BIGINT (20),and Pojo use "Long".
When saving to the database, use a timestamp of length 13, Only need to convert to timestamp when receiving the parameter, and convert the timestamp to any format when returning data.
You could iterate over all your date formats and try to parse the input to a date.
If you can parse exactly one input to a date then you are fine.
If you cannot parse the input, then you need to add a date format to your list.
If you can parse the input to more than one date and the dates are not the same, then you need to think how to proceed :-D
The code could look like
private Date parse(String dateValue) {
final List<SimpleDateFormat> simpleDateFormats = Arrays.asList(
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("dd.MM.yyyy"));
final List<Date> dateCandidates = simpleDateFormats.stream()
.map(formatter -> tryParse(formatter, dateValue))
.filter(Objects::nonNull)
// check if there is more than one date
.collect(Collectors.toList());
if (dateCandidates.isEmpty()) {
throw new RuntimeException(String.format("Unsupported date format %s", dateValue));
}
if (dateCandidates.size() > 1) {
// check if all dates are the same, otherwise throw exception since your input is ambigious
}
return dateCandidates.get(0);
}
private Date tryParse(SimpleDateFormat formatter, String dateValue) {
try {
return formatter.parse(dateValue);
} catch (ParseException e) {
return null;
}
}
I'm using JPA and I want to store the date in this format dd/MM/yyyy HH:mm:ss
So I create a function
public static String getNowDate() {
Date date = new Date();
final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
return sdf.format(date);
}
The problem is that this returns a String and I need a date.
#Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
How do I make this work so I can save date and time exactly like that?
I know a easy solution is to declare creationDate as String. Is this too bad?
There is a problem with the premise to your question. Ideally, your current timestamp will be stored in a SQL database, in some sort of date column, and not as text. Since you are using JPA, backed by JDBC, you should just be inserting a date type. So, something like the following should work:
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// or maybe just
Date now = new Date();
Then, just let JPA worry about how to martial the current timestamp into your database table. If you really need to format your timestamp as shown, then worry about this in your Java code somewhere.
I am somewhat new to Java and am trying to write a query which will return the documents stored in my mongodb between two dates. I think I am close to getting it correct but am having a hard time getting there. I am receiving the date ranges in the following format:
[2017-06-29, 2017-07-05]
The dates that i am comparing are stored in the database as follows:
yyyy-MM-dd'T'HH:mm:ss-SSSS
What i have so far..
public List<VehicleStatus> getReportingDateRange(List<String> dates) {
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-SSSS");
Date startDate = outputFormat.parse(dates.get(0));
Date endDate = outputFormat.parse(dates.get(1));
Query query = new Query();
Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(outputFormat.format(startDate)),
Criteria.where("updateTime").lte(outputFormat.format(endDate)));
query.addCriteria(c);
return this.mongoOperations.find(query, VehicleStatus.class);
}
I am receiving a parse Exception. I am really not sure where to go from here, any help is greatly appreciated. If you need any additional information please let me know.
Thank you!
You will need to parse the date into input format followed by formatting the date to output format.
Something like
String startDate = outputFormat.format(inputFormat.parse(dates[0]));
String endDate = outputFormat.format(inputFormat.parse(dates[0]));
Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(startDate), Criteria.where("updateTime").lte(endDate));
You should try to save the date as date type and use below version.
Date startDate = inputFormat.parse(dates.get(0));
Date endDate = inputFormat .parse(dates.get(1));
Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(startDate), Criteria.where("updateTime").lte(endDate));
I have two jDateChooser on my dialog , I want to save to MS-SQL DB having issue with that data types. Any idea how to fix this issue ! I can only do this when i convert data type to nvarchar in DB and convert the value to string which returns from jDateChooser.
// I can save in this way but I it doesn't use jDateChooser;
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());
// I cant save the date with jDateChooser
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate());
// Only Way I found
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sd = dateFormat.format(jDateChooser3.getDate());
obj.setStartDate(sd);
//
Judging from the code you posted it looks like jDateChooser3.getDate() returns a java.util.Date instance while the java.sql.Date(millis) constructor expects the date/time as a long milliseconds value.
Use this code and it will work:
java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate().getTime());
Since it comes from a date chooser component, invalid input most likely results in null returned date, so you might want to also check on that:
java.util.Date d = jDateChooser3.getDate();
if (d == null) {
System.out.println("No date specified!");
} else {
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// Do something with sqldate
}
Right click on jDateChooser. Go to the Properties and dateFormatString. Set this format: dd-MM-yyyy.
Date keyword = jDateChooserattendance.getDate();
java.sql.Date sqldate = new java.sql.Date(keyword.getTime());
forum member
I am having one problem with date time in java. Actually I am receiving the startdate in format 2012-02-27T01:10:10 and I want to insert the received date to my database having datetime datatype.
Actually I tried to convert the startdate received to datetime by below code
String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startD = sdf.format(sDate);
Date endD = sdf.format(eDate);
but with the above code only date gets added to my database like 2012-02-27 00:00:00
I want to add the time also to my database but when I change the SimpleDateFormat to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); nothing works.
please suggest me some solution I can apply so my time also gets added to database. I am using Hibernate JPA as my persistence layer.
SimpleDateFormat's format() method doesn't return a Date type.
try this:
Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(sDate);
Try this,
yyyy-MM-dd'T'HH:mm:ss
String sDate = jsonObject.get("StartDate").toString();
String eDate = jsonObject.get("EndDate").toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startD = sdf.format(sDate);
Timestamp startTime = new Timestamp(startD.getTime());
Date endD = sdf.format(eDate);
Timestamp endTime = new Timestamp(endD.getTime());
Of course only the date is parsed, since the pattern you provided to the SimpleDateFormat constructor only contains the date part! Add the time part to it and it will parse the time too just fine.
you can try like this....
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");