I'm like two days on this problem. This is in continuation from this question: convert string to joda datetime gets stuck
I am trying to perform one simple task, to take SQL SERVER ISO 8601 string and convert it to DateTime or LocalDate. I tried everything, but when it comes to the actual conversion with the debugger the program cursor just goes for thinking and never comes back, i.e., the program is stuck there.
I'm so desperate that I even tried to convert that String to Date (which works!) and then convert that Date to DateTime or LocalDate. All had the same result, on the line of the conversion the program looks like stuck in some endless loop (and the fan starts working like crazy).
Here is my so simple function:
public static LocalDate SQLServerIso8601StringToLocalDate(String date) {
LocalDate dateToReturn = null;
DateFormat df = new SimpleDateFormat(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT);
try {
Date tempDate = (Date) df.parse(date);
DateTime dt = new DateTime(tempDate);
dateToReturn = new LocalDate(tempDate);
} catch (ParseException e) {
// strToReturn = strSqlDate;
}
return dateToReturn;
/*
date = date.replace('T', ' ');
return SimpleIso8601StringToDateTime(date);
*/
//return LocalDate.parse(date, DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT));
/* DateTimeFormatter df = DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT_WITH_TZ);
return df.parseDateTime(date);
*/ }
This is my String: 2014-01-01T00:00:00
also:
public static final String SQL_SERVER_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
Any ideas?
public static Date getDateFromString(String format, String dateStr) {
DateFormat formatter = new SimpleDateFormat(format);
Date date = null;
try {
date = (Date) formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Related
I am trying to parse a DateTime from C# to Java. Here is what comes as string from the java call "2018-08-02T14:24:40.040353". When I try to parse it, I get the following error Unparseable date: "2018-08-02T14:24:40.040353"
Here is the method for parsing the date
public static String dateFormater(String dateFromJSON, String
expectedFormat, String oldFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date date = null;
String convertedDate = null;
try {
date = dateFormat.parse(dateFromJSON);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat(expectedFormat);
convertedDate = simpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return convertedDate;
}
Your date look like a ISO_LOCAL_DATE_TIME, If you are using Java8 you can use java.time API and use the default format of LocalDateTime like this :
String dateString = "2018-08-02T14:24:40.040353";
LocalDateTime ldt = LocalDateTime.parse(dateString);
ldt.toString():
2018-08-02T14:24:40.040353
How can I obtain the timezone format and return the string like so
https://msdn.microsoft.com/en-us/library/gg154758.aspx - column Time zone name,
for example "SA Pacific Standard Time"
or worse comes to worst, how can I obtain it like so: (UTC-05:00) Bogota, Lima, Quito, or at least (UTC-05:00)
so that I can manually match it to the former String if I put them all in a map?
Have you tried?
TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
Result be if your device happens to be in Australia
TimeZone GMT+09:30 Timezon id :: Australia/Darwin
This should return UTC as a String and Date object. You can change the date format.
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"
public static Date getUTCdatetimeAsDate()
{
//note: doesn't check for null
return stringDateToDate(GetUTCdatetimeAsString());
}
public static String getUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date stringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
This might be irrelevant (don't know if Android fully supports java-8), but you can do it using standard java api:
ZonedDateTime dateTime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Asia/Tokyo]");
System.out.println(dateTime); // 2007-12-03T10:15:30+09:00[Asia/Tokyo]
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("(O) z");
System.out.println(dateTime.format(formatter)); // (GMT+9) JST
I am writing a converter method that would parse an xml string data to java objects. But i am not able to parse dates to date objects.
How to format this date string "2013-08-26T12:00:00.000" in the following way: "2013-08-26 12:00:00" to Date object in java?
Edited to add the below code snippet.
Here is what I tried to do.
public Object fromString(String str) {
DateFormat dateFormat = DateFormat.getDateInstance();
try {
Date date = dateFormat.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
}
You should google these things first.
Here you go:
public static void main(String[] args) {
String inFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
String outFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
try {
Date d = sdf.parse("2013-08-26T12:00:00.000");
sdf.applyPattern(outFormat);
System.out.println(sdf.format(d));
} catch (ParseException e) {
// handle appropriately
e.printStackTrace();
}
}
You can parse any Date-like string to Date object (as long as the the string represents a valid date) using http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
There is no point in re-formatting the String.
In order to process XML-date-time-strings which are allowed to be of variable precision (sometimes leaving out second- or fraction-part or timezone-offset) the use of SimpleDateFormat is not a good option because then you would only have one pattern. Not flexible.
Alternative for XML:
String xml = "2013-08-26T12:00:00.000"; // maybe optionally with additional timezone offset
javax.xml.datatype.DatatypeFactory factory = javax.xml.datatype.DatatypeFactory.newInstance();
XMLGregorianCalendar xmlGregCal = factory.newXMLGregorianCalendar(xml);
java.util.Date d = xmlGregCal.toGregorianCalendar().getTime();
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output = outputFormat.format(d);
Watch also out for some overloaded methods to change the timezone settings for parsing and formatting - see the javadoc.
You may try this:
public static void main(String[] args) throws Exception {
String original = "2013-08-26T12:00:00.000";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S").parse(original);
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.printf("Before: %s\nAfter: %s\n", original, format);
}
OUTPUT:
Before: 2013-08-26T12:00:00.000
After: 2013-08-26 12:00:00
I receive date as string from web service in following format 2014-02-27T11:17:00.000Z Could someone tell me how to parse it as Date time object in Java.
I tried parsing it Date.parse() but it didn't work properly.
Then I tried date formatter but it crashes the app. Could someone enlighten me please.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date d = sdf.parse("2014-02-27T11:17:00.000Z");
You can use dateformat class for that.
DateFormat sdt = new SimpleDateFormat(put your format here);
Date stime= sdt.parse(starttime);
Date etime = sdt.parse(endtime);
Starttime and end time are the strings which you want to parse
Declare a SimpleDateTimeFormat to match your datetime from C# and then use .parse() method on it to get the (Java) Date.
Example:
private static final SimpleDateFormat FORMAT_FULL_DATE = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss'.000Z'"); // replace kk with hh for am/pm format
public static Date getDateTimeFromString(final String string) {
try {
return FORMAT_FULL_DATE.parse(string);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
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");