I have a date in the following format in UI..
Eg: Thu. 03/01
I convert them to XMLGregorianCalendar as below explained.
final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = closeDate;
final Date dDate = format.parse(dateStr);
GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(dDate);
XMLGregorianCalendar dealCloseDate = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(gregory);
My Output is "3/06/70 05:00 AM" instead of "3/06/2011 05:00 AM". What is the chnage required to get the proper year.
You didn't mention anything about how the year is supposed to be represented in this date conversion, but here is some pseudocode to get you started. Note that I don't explicitly deal with the timezone in this example:
final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = "Thu. 03/01";
final Date date = format.parse(dateStr);
GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(date);
XMLGregorianCalendar calendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(
gregory);
I did the following code to achieve the same. May be a lengthy code but is working fine.
def gregorianCalendar = new GregorianCalendar()
def xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
if (//past date is needed) {
def date = xmlGregorianCalendar.toGregorianCalendar().time
def cal = Calendar.getInstance()
cal.setTime(date)
cal.add(Calendar.DATE,-3); //subtract 3 days
date.setTime(cal.getTime().getTime())
gregorianCalendar.setTime(date)
xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
}
Related
I am trying to convert String to gregoriancalendar date, it unable to convert. because, the string has different format. like '2015-05-22T16:28:40.317-04:00'. I have seen some of the other examples, but they are not in this time format.
I am using something like below:
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz").parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
I even tried like this too:
gregory.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(sampleDate));
If you check SimpleDateFormat doc, you will see that there's no T in the format pattern. In order to escape non-pattern characters, wrap them around single quotes ' as shown in this example (taken from the docs):
"hh 'o''clock' a, zzzz" -> 12 o'clock PM, Pacific Daylight Time
I think the proper format should be this:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
// ^-^-----check these
// don't pay attention to the smiley generated above, they're arrows ;)
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat(format).parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
This works as well
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-05-22T16:28:40.317-04:00");
GregorianCalendar gregorianCalendar = xmlGregorianCalendar.toGregorianCalendar();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz");
//dateFormat.setTimeZone(TimeZone.getTimeZone());
Date inputDate = dateFormat.parse(inputDatetime);
GregorianCalendar c = new GregorianCalendar();
c.setTime(inputDate);
XMLGregorianCalendar outputDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
return outputDate;
} catch (ParseException | DatatypeConfigurationException e) {
log.error("exception: {}", e.getMessage());
return null;
}
I have this class:
public class Period {
private Date startDate = new Date();
private String startTime = "10:00";
private Date endDate = new Date();
private String endTime = "23:59";
}
Now i need two string outputs "2014-01-08T10:00:00" and "2014-01-08T23:59:00" !
How i can create these two outputs by using the class Period? It is necessary to work with the class Period, which includes startTime and EndTime as Strings!
I only have this small code for using the class SimpleDateFormat:
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Thanks for helping me !
What #tobias_k is saying is this. This is your format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
When you call .format to format the Date, it returns a String
String dateString = format.format(period.getStartDate());
Then concatenate it
dateString = dateString + period.getStartTime();
Try this: Using GregorianCalendar
Date startDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 10, 00, 00);
System.out.println(dateFormat.format(cal.getTime()));
Update1
To get the string value in variable:
String str = dateFormat.format(cal.getTime());
System.out.println(str);
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.
Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);
I want to increment the milliseconds in any given date in the format yyyymmddhhmmss.mmm in each iteration. mmm here represents milliseconds. And I want to perfom this operation in Java 1.5.
For example: 20120823151034.567 should be incremented to 20120823151034.568
You can use long milli-seconds which make incrementing trivial.
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date d = df.parse(input);
d.setTime(d.getTime()+1);
System.out.println(df.format(d));
I wouldn't use Calendar as its very slow.
You can parse String to Date object and use getTime() and setTime(long l) to modify date. Then you can convert Date object back to String. For parsing String and converting Date object back to String you can use SimpleDateFormat class.
The best class to use for this operation is Calendar. You set it to the desired date, and then use
myCalendar.add(Calendar.MILLISECOND, 1);
to advance it by one millisecond. Use DateFormat to produce string representations.
This gives you what you want. It will work across any day/month/year boundary, as well as handling the start and end of daylight saving time.
final String input = "20120823151034.567";
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
final Calendar c = Calendar.getInstance();
c.setTime(df.parse(input));
c.add(Calendar.MILLISECOND, 1);
System.out.println(df.format(c.getTime()));
You can use Calendar Class as well as Date Class for this....
Date Class:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = format.parse(input);
date.setTime(date.getTime()+1);
System.out.println(format.format(date));
Calendar Class:
final String dateStr = "20120823151034.567";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(dateStr ));
c.add(Calendar.MILLISECOND,1);
System.out.println(format.format(c.getTime()));
In both cases, format.parse() has the potential to throw a ParseException, which you will need to catch and handle.
An alternative without using Calendar (although, Calendar is fine)
final String input = "20120814120000.111";
final DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
Date date = new format.parse(input);
long time = date.getTime();
Date incrementedDate = new Date(time + 1);
System.out.println(format.format(date));