I'm kinda new to GWT.
I have been trying to parse a date in the format "dd-MMM-yyyy HH:mm z" but I get an exception when the timezone is EDT or BST.
Doesn't GWT support these timezones while parsing? If so, is there any alternative way which I can use to parse the date using EDT?
Please help.
Code snippet:
DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm z");
String fomattedDate = dateParser.format(date,Timezone.createTimeZone(TimeZoneConstants.americaNewYork));
Date newDate = dateTimeParser.parse(formattedDate);
This line gives me exception.
After reading the docs for DateTimeFormat it said that it supports a lower number of timezones while parsing.
This code works for me:
TimeZoneConstants timeZoneConstants = GWT.create(TimeZoneConstants.class);
DateTimeFormat dateParser = DateTimeFormat.getFormat("dd-MMM-yyyy HH:mm Z");
String formattedDate = dateParser.format(date, TimeZone.createTimeZone(timeZoneConstants.americaNewYork()));
Date newDate = dateParser.parse(formattedDate);
I have created TimeZoneConstants via deferred binding and changed the format to dd-MMM-yyyy HH:mm Z (capital Z) - with z I did got an IllegalArgumentException indeed.
If you still get issues, try inspect deeply the parsing function to track down the exact problem.
Related
SimpleDateFormat df = new SimpleDateFormat();
Date lastLogin = null;
try {
String troubleChild = lineScanner.next();
lastLogin = df.parse(troubleChild);
} catch (ParseException e) {
System.out.println("ohnoes");
}
Hi I'm quite new to using the date functions and I've come up with a problem. I have a file that is being parsed into various variables and they all work except this one i can never get it so that it passes the try/catch clause i've looked up similar problems but none of them work on my code.(The date i am inputting is in the format: Mon, Oct 30 22:20:11 GMT 2017) please can I get some help and thanks for it!
Solution: java.time
Please don’t take the trouble with the long outmoded classes Date and SimpleDateFormat. Instead use java.time, the modern Java date and time API also known as JSR-310:
DateTimeFormatter dtf
= DateTimeFormatter.ofPattern("E, MMM d H:mm:ss z uuuu", Locale.UK);
String inputDate = "Mon, Oct 30 22:20:11 GMT 2017";
ZonedDateTime lastLogin = ZonedDateTime.parse(inputDate, dtf);
System.out.println(lastLogin);
This prints
2017-10-30T22:20:11Z[GMT]
Since dates and times may come in so many different textual formats, I am using a format pattern string to specify your particular format. For which letters you may use, and what difference it makes whether you use 1, 3 or 4 of the same letter, see the documentation. Beware that format pattern strings are case sensitive.
Problem: SimpleDateFormat
You used the no-arg SimpleDateFormat constructor. The way I read the documentation, this gives you the default date format for your locale. If your JVM is running UK locale, I believe the format goes like 28/11/17 10:57 — not much like the input format you were trying to parse. You can use System.out.println(df.format(new Date())); to find out. The usual SimpleDateFormat constructor to use would be SimpleDateFormat(String, Locale) so that you may again supply a format pattern string and a locale.
I want to parse a java Date object to the format like below
I tied the below codes
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dfz = new SimpleDateFormat("dd MMM yyyy");
Date date = new Date();
String d = dfz.format(date);
System.out.println(d);
then the out put will be like
I checked this SO question and using that I could get the below format
Is there any built-in functionality in java to parse a date object to the first where the th is a superscript format or do I need to do it manually ? And how to do it manually ?
What is the easiest way to do this ?
Based on the UI you are using you need to handle super script separately.
for HTML simply use tag 17th (17<sup>th</sup>)
I'm fetching a date which is of String type but I need the Date type of it. I'm working in a gwt project so I cannot use SimpleDateFormat or Calendar (which doesn't work in the runtime and throws source code not found exception)
new Date(stringDate) is deprecated so any other way I could do it?
Consider using DateTimeFormat:
http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html
check this
String myString = "Thu Nov 18 11:53:45 BST 2014";
DateTimeFormat sdf = DateTimeFormat.getFormat("EEE MM dd HH:mm:ss z yyyy");
Date myDate = sdf.parse(myString);
For more details check this
I hope this could help!
You can use the parse() function of DateTimeFormat class from com.google.gwt.i18n.client package
http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/shared/DateTimeFormat.html#parse%28java.lang.String%29
Assuming you have a Date (java.sql.Date) in the format yyy-mm-dd you can call
Date.valueOf("1996-12-12") which returns a Date instance
If you use Date (java.util.Date) you can call
new Date(System.currentTimeMillis()) which returns a Date instance
Edit
GWT has DateTimeFormat which you can use
Okay, so here's my issue in Android right now. On our Database there's a timestamp in this format 8/15/2013 2:00:48 PM and through a .NET WebService I get that same time like this in Android: 2013-08-15T14:00:48-07:00. Now I want to convert this format into a Date Time format that I can use for comparison (for example this webservice provides every instance where a device failed at logging in so we want to check the amount of time between occurances to see if there's any issues). Below I have this code where I'm trying to use JODA Time but it's still not returning the correct format:
public static Date convertStringToDate(String input) {
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
DateTime dateTime = formatter.parseDateTime(input);
return dateTime.toDate();
//printout shows: Thu Aug 15 17:00:48 EDT 2013
}
I know that the server is returning some crappy time format that is hard to work with (it took a while to get this to work in the iOS App we have, and even there it's still rather clunky) so I don't mind changing the webservice or the query if that would make things easier.
I have a very similar format, and I parse it using SimpleDateFormat, try this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ", Locale.US);
Date dateTime = format .parse(value);
What i understand is that you have your correct instance of date already and what you need is to parse it to String.
I suggest you use:
DateFormat formatter = new SimpleDateFormat("d/MM/yyyy hh:mm:ss a");
//this will give you the format '8/15/2013 2:00:48 PM'
String d = formatter.format(date);
Hope this helps.
EDIT:
Also seams you want to have your date instance in -07:00 timezone
So you can change your line
DateTime dateTime = formatter.parseDateTime(input);
for
DateTime dateTime = formatter.withZone(DateTimeZone.forID("-07:00")).parseDateTime(input);
I am trying to use setDate() in a PreparedStatement, however the date that I have is in the format of 2008-07-31. The code is:
pstmt.setDate(f++, (Date) DateFormat.getDateInstance(DateFormat.DEFAULT).parse(value.substring(0, 10)));
However, it gives me the error:
Exception in thread "main" java.text.ParseException: Unparseable date: "2008-07-31"
Why is this?
If you have a very specific date, don't ask Java to use a default date format - set it yourself.
For example:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(value.substring(0, 10));
You should also potentially set the time zone of the parser... my guess is that UTC is the most appropriate time zone here.
Note that this has nothing to do with prepared statements as such - it's just date parsing.
(As an alternative to using DateFormat and SimpleDateFormat, you could use Joda Time which has a nicer API and thread-safe formatters/parsers. You can ask Joda Time to convert from its own types to Date values. Possibly overkill if you only need it for parsing here, but if you're doing anything else with dates, it's well worth looking into.)
You need make sure the default DateFormat is in yyyy-MM-dd format (usually it's a config in OS), or you can use SimpleDateFormat or java.sql.Date to parse date string.
java.util.Date d;
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
d = sdf.parse ("2008-07-31");
// or
d = java.sql.Date.valueOf ("2008-07-31");
or, you could just set parameter as String, if the underlying database driver support the VARCHAR/CHAR to DATE conversion.
DateFormat.DEFAULT points to MEDIUM format and MEDIUM format looks like Jan 12, 1952. So, you may have create a SimpleDateFormat object with the format you are using.
I think there is mismatch in the format of the date that you are providing as input and the format in which you have specified while formatting which is default in your case.
SimpleDateFormat parser = new SimpleDateFormay("yyyy-MM-dd");
Try using the same format for both the dates.
First convert String to Date and then set that to PreparedStatement. Check with below code.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date convertedDate = dateFormat.parse(dateString);
I'd use
pstmt.setDate(f++,
new java.sql.Date(
new SimpleDateFormat("yyyy-MM-dd")
.parse(value.substring(0, 10))
.getTime()
)
);