Currently I get 20150211152026.0Z this format from ldap now I would like to store this in my database in this format YYYY-MON-DD HH:mm:ss with java.
Please guide how this could be achieved.
As specified in this question, you can do sthg like this;
String[] parts = inputDateTime.split("[.]");
String dateTimePart = parts[0];
String timeZonePart = "+0" + parts[1].substring(0, parts[1].length() - 1) + "00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssZ");
Date theDate = sdf.parse(dateTimePart + timeZonePart);
Thanks to highlycaffeinated
Updated version for your comment;
String inputDateTime = "20150211152026.0Z";
String[] parts = inputDateTime.split("[.]");
String dateTimePart = parts[0];
String timeZonePart = "+0" + parts[1].substring(0, parts[1].length() - 1) + "00";
DateFormat originalFormat = new SimpleDateFormat("yyyyMMddHHmmssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = originalFormat.parse(dateTimePart+timeZonePart);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
Related
How to obtain Date values from database then set it as a string. Can anyone tell me why the below code is not working properly? This is the portion giving trouble:
// Date d = rs.getDate("PatientBirthDate");
// DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// String date = df.format(d);
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
// dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}
Why you are reading the date outside the loop?
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
Date d = rs.getDate("PatientBirthDate");
String date = df.format(d);
dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}
Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
I do not know why different input but the output is duplicate, here is my code
Date d = new Date(1409716800);
Date d1 = new Date(1409716801);
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy-HH:mm:ss");
String formattedDate = sdf.format(d);
String formattedDate1 = sdf.format(d1);
Log.d("time", formattedDate);
Log.d("time", formattedDate1);
The output is
10-24 06:12:50.508: D/time(29097): 17.01.70-07:35:16
10-24 06:12:50.508: D/time(29097): 17.01.70-07:35:16
can anyone tell me why the output are duplicate? My timezone is GMT+7
Date d = new Date(1409716800);
Date d1 = new Date(1409716801);
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy-HH:mm:ss:SSS");
String formattedDate = sdf.format(d);
String formattedDate1 = sdf.format(d1);
Log.d("time", formattedDate);
Log.d("time", formattedDate1);
Ouput:
17.01.70-14:35:16:800
17.01.70-14:35:16:801
I used two calender for starting date and finishing as below,
date.Ic.add(Restrictions.between("islemZamani", date1, date2));
However result of this criteria has results of date1 and between of them,not include results of date2.I mean ,It shows date1<= results. I want date1<= results<=date2. So I tried SimpleDateFormat like this;
String tar1 = new String();
String tar2 = new String();
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
dateFormat.setTimeZone(tz);
tar1 = dateFormat.format(date1);
tar2 = dateFormat.format(date2);
tar1 = tar1.substring(11, 18) + "000000";
tar2 = tar2.substring(11, 18) + "235959";
c.add(Restrictions.between("islemZamani", tar1, tar2));
Now It gives NullPointerException.How can i solve this problem? Do you suggest any different way from SimpleDateFormat? Thanka for any reply.
Use a string to manipulate date is too complicated and may case a loooot of problems.
Use only java.util.Date and/or java.util.GregorianCalendar.
Try this :
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.setTime(date1);
// Edit the calendar1 here if you want to
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.setTime(date2);
calendar2.add(GregorianCalendar.DAY_OF_YEAR, 1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
dateFormat.setTimeZone(tz);
String tar1 = dateFormat.format(calendar1.getTime());
String tar2 = dateFormat.format(calendar2.getTime());
c.add(Restrictions.between("islemZamani", tar1, tar2));
But without the trace, I'm not sure this will solve your problem...
And, if it's possible, use the date instead of the string in your restriction.
I get the date format from a joynet cloud api server:
2012-11-20T10:26:04+00:00"
However, I have no idea to handle the last segment +00:00, I have made the format except for +00:00
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = fmt.parse("2012-11-20T10:26:04");
Thanks for #Abu
I rewrite it to remove ":",
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String input = "2012-11-20T10:25:58+00:00";
String s1 = input.split("T")[0];
String s2 = input.split("T")[1];
String sep = null;
if (s2.contains("+")) {
sep = "+";
}
if (s2.contains("-")) {
sep = "-";
}
String s3 = s2.split("\\" + sep)[0];
String s4 = s2.split("\\" + sep)[1].replace(":", "");
String cleanDate = s1 + "T" + s3 + sep + s4;
Date date = fmt.parse(cleanDate);
System.out.println(date);
Remove that : inside the time zone part if you are not using java 7
and use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
See javadoc for SimpleDateFormat in Java 6
And
If you are using Java 7 then directly use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
See javadoc for SimpleDateFormat in Java 7