I am getting a Date object, which i need to convert to XMLGregorian Calendar specific format
I tried below ways
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(formattedDate);
dataListType.setTimestamp(xmlCal);
I am getting an exception, for sure I am doing wrong here. But I want to format the Date object into specified format, which is done by sdf.format perfectly.
But how do I create the XMLGregorianCalendar object for the same (from formattedDate)?
You should fixed your date format:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = sdf.format(new Date());
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(date);
You can do it by the date object itself:
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
convertStringToXmlGregorian(formattedDate);
public XMLGregorianCalendar convertStringToXmlGregorian(String dateString)
{
try {
Date date = sdf.parse(dateString);
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (ParseException e) {
// Optimize exception handling
System.out.print(e.getMessage());
return null;
}
}
Related
I have a Date and I need to change timezone of this date to UTC. The code below does not work.
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(dateLocal.getTime());
return new Date(cal.getTimeInMillis());
On the Stackoverflow all examples return either a String or use API 26. How can I solve my problem on the Android API 21?
Try this:
public Date getDateInUtc() {
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
Date dateToReturn = null;
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
String utcTime = sdf.format(new Date());
try {
dateToReturn = dateFormat.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
return dateToReturn;
}
Use cal.getTime(); which returns date
The same result you can get without SimpleDateFormat:
public Date dateInUtc(Date input) {
Calendar inputCalendar = Calendar.getInstance();
inputCalendar.setTime(input);
TimeZone timeZone = inputCalendar.getTimeZone();
long timeInUtc = input.getTime() - timeZone.getRawOffset();
Calendar outputCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
outputCalendar.setTimeInMillis(timeInUtc);
return outputCalendar.getTime();
}
I'm trying get current time and then, convert to UTC+1 time zone which is in yyyy-MM-dd HH:mm:ss format
I tried the following code. It didn't work. Can anyone help?
public static String getDateTime() {
Date time = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC+1"));
return outputFmt.format(time);
}
I think you can try:
public static String getDateTime() {
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Read current time already in desired timezone
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
return outputFmt.format(time.getTime());
}
Following method will add one hour to UTC time.
public static String getDateTime()
{
SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
String utctime=outputFmt.format(new Date());
String time=null;
Calendar calendar=null;
Date date;
try
{
calendar = Calendar.getInstance();
calendar.setTime(outputFmt.parse(utctime));
calendar.add(Calendar.HOUR, 1);
date=calendar.getTime();
time=outputFmt.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
I am trying to get the current date in a Talend job and I am using this as my context variable:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
context.dateout = dateFormat.format(date);
System.out.println(context.dateout);
However, the type of the result is a String and not a Date.
How should I correct it?
Thank you very much!!
Try to do that according the following code:
String string = "2016-03-15";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try {
Date date = dateFormat.parse(string);
System.out.println(date);
} catch (ParseException ex) {
System.out.println(ex);
}
I don't know what your context.dateout means.
Note the difference between parse and format.
This is to create a string from a date:
dateFormat.format(date);
This is to create a date from a string:
dateFormat.parse(dateString);
Response from jsp is coming in this format: "mm/dd/yyyy hh:mm", and I want to convert to db format "yyyy-mm-dd hh:mm:ss".
I tried this code :
public java.sql.Date getdateFormat(String datestring) throws ParseException {
String datestr = "";
try {
java.util.Date date = new SimpleDateFormat("mm/dd/yyyy hh:mm a",
Locale.ENGLISH).parse(datestring);
atestr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
} catch (Exception e) {
e.printStackTrace();
}
return getDateFromString(datestr);
}
public java.sql.Date getDateFromString(String string) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date finalDate = null;
try {
finalDate = sdf.parse(string);
} catch (Exception e) {
e.printStackTrace();
}
return new java.sql.Date(finalDate.getTime());
}
A common mistake in using SimpleDateFormat is skip the documentation and assume that is knows when mm means months and when mm mean minutes. It doesn't. mm only means minutes. If you want months use MM Also only use a if you expect AM/PM and only use hh for 12 hour clocks. I would expect your format should read
MM/dd/yyyy HH:mm
and your output
yyyy-MM-dd HH:mm:ss
BTW You shouldn't need to convert to a String to use JDBC. Using a Date is faster and less error prone.
If all you want is the Date then you do not need to do
atestr=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
simply return the date as this stage.
A date does not have any formatting, it is basically a number.
I would basically do it like this
String dateInString = "20140611";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date toDate = sdf.parse(dateInString);
sdf = new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = String.format(sdf.format(toDate));
System.out.println(tmpStr);
We can get the date in the following way
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parseDate = dfm.parse(datestring);
Now the parseDate is in the format "yyyy-MM-dd HH:mm:ss"
I have a date in(string) in dd-mon-yyyy format and I want to compare this date with system date.
eg.
I have 12-OCT-2010
and I want to compere this with system date in same format
You can use the SystemDateFormat class to parse your String, for example
final DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
final Date input = fmt.parse("12-OCT-2010");
if (input.before(new Date()) {
// do something
}
Note that SimpleDateFormat is not threadsafe, so needs to be wrapped in a ThreadLocal if you have more than one thread accessing your code.
You may also be interested in Joda, which provides a better date API
Use SimpleDateFormat http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
String d = "12-OCT-2010";
try {
Date formatted = f.parse(d);
Date sysDate = new Date();
System.out.println(formatted);
System.out.println(sysDate);
if(formatted.before(sysDate)){
System.out.println("Formatted Date is older");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I would recommend using Joda Time. You can parse that String into a LocalDate object very simply, and then construct another LocalDate from the system clock. You can then compare these dates.
Using simpledateformat -
String df = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(df);
Calendar cal = Calendar.getInstance();
/* system date */
String systemdate = sdf.format(cal.getTime());
/* the date you want to compare in string format */
String yourdate = "12-Oct-2010";
Date ydate = null;
try {
ydate = sdf.parse(yourdate);
} catch (ParseException e) {
e.printStackTrace();
}
yourdate = sdf.format(ydate);
System.out.println(systemdate.equals(yourdate) ? "true" : "false");