Let's say I have a text file with names, an order number, and a date an order was placed. If the dates are in the format of MMDDYY (080315 = August 3, 2015 for example) is there a way I can parse that, convert into date format, then compare it to the current date? I want it so that if they submit a date in this format that is after the current day, it is invalid.
I would recommend using the Joda-Time library.
Joda-Time Website
Then you can do something like.
DateFormat df = new SimpleDateFormat("MMddyy");
Date date = df.parse("070791");
LocalDate ld = new LocalDate(date);
Related
I have a query for an Oracle database that returns a datetime column. In the java method, the column is converted to a string.
A portion of the code looks like this:
ResultSet rs;
HashMap<String, String> hm=new HashMap<String, String> ();
hm.put("SchEndDate2", rs1.getString("END_DT_TM_GMT"));
When I view the strings value in the debugger it looks like this: "2019-07-04 11:00:00.0"
I need to convert this string to the datetime format of this: "yyyy-MM-dd'T'HH:mm"
I tried this SimpleDateFormat to complete this but when I convert the string to the format it returns the dateTime in Eastern Daylight Time and not GMT.
The value after going thru the conversion is this: "Thu Jul 04 07:00:00 EDT 2019"
This is the code that I am using to convert the string to a DateTime.
EndDate=map.get("SchEndDate2");
//EndDate : **"2019-07-04 11:00:00.0"**
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date databaseDateTime = formatter.parse(EndDate);
//databaseDateTime: **"Thu Jul 04 07:00:00 EDT 2019"**
Why is the format incorrect and the timezone not correctly set?
Two points.
Don’t fetch your date and time as a string from Oracle. Fetch a proper date-time object. In this case a LocalDateTime.
Don’t use SimpleDateFormat, TimeZone and Date. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Use java.time, the modern Java date and time API.
In code:
ResultSet rs = // …;
LocalDateTime dateTime = rs.getObject("END_DT_TM_GMT", LocalDateTime.class);
String databaseDateTimeAsString = dateTime.toString();
System.out.println(databaseDateTimeAsString);
Example output:
2019-07-04T11:00
It’s not quite the output format that you asked for, but it most likely will serve your purpose. The format you asked for is ISO 8601. So is the output I have given you. In the ISO 8601 standard, including the seconds and fraction of second when they are 0 is optional. If you insist on including them, use a formatter. For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
String databaseDateTimeAsString = dateTime.format(formatter);
2019-07-04T11:00:00.000
Using rs.getObject() for getting a LocalDateTime as shown requires a JDBC 4.2 compliant database driver. You probably have got that. In case you haven’t and you cannot upgrade, use:
LocalDateTime dateTime = rs.getTimestamp("END_DT_TM_GMT").toLocalDateTime();
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
Try converting the Date object to an Instant. Here's an example showing your input string first converted to a Date, and then converting that to an Instant. The date contains the timezone-specific rendering, but the instant does not.
String input = "2019-07-04 11:00:00.0";
System.out.println("input: " + input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = formatter.parse(input);
System.out.println("date: " + date);
Instant instant = date.toInstant();
System.out.println("instant: " + instant);
And here's the output:
input: 2019-07-04 11:00:00.0
date: Thu Jul 04 05:00:00 MDT 2019
instant: 2019-07-04T11:00:00Z
I am working with JodaTime now and have a question about how to parse String into DateTime.
I have got a date String in the format:
"2013-05-14T11:36:08+0000"
I tried to convert it into JodaTime's DateTime object:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dateTime = fmt.parseDateTime("2013-05-14T11:36:08+0000");
It works fine except that if I call
dateTime.getHourOfDay()
It returns me 12 instead of 11.
Surprisingly, if I use the Java's SimpleDateFormat:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = df.parse("2013-05-14T11:36:08+0000");
The date contains exactly the same result, the hour is 12 instead of 11.
I am based in London. I started to think whether this is because the summer saving time? Or did I make an mistake on how the String should be parsed?
Please help. Many thanks.
I get dates as Strings (ie: 2013-04-07 17:20:16.0) and I need to create Date objects to represent these so that I can set the date's in JSpinners.
I am using this to format the date strings I get:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
I am using it like this:
df.setLenient(false);
Date tempDateOld = new Date();
tempDateOld = df.parse("2013-04-07 17:20:16.0");
However this:
System.out.println(tempDateOld.toString());
gives this:
Sun Apr 07 17:20:16 CAT 2013
Why does it not just give me a Date with the date in the same format? How do I take a date of a given format and create a Date object with the date of the same format.
Any help will save my sanity, thanks.
Try
System.out.println(df.format(tempDateOld));
The date by itself does not keep the format.
I am developing a spring application and in one of my controller i have following lines to parse from string to date and format the parsed date to required format. But again i need to parse back formatted string into date without using any SimpleDateFormat, so is it possible to do so ?
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
Date pick=dateFormat.parse(request.getParameter("pickDate"));
String pick_date=dateFormat2.format(pick);
Edit:
I found in the wikipedia that china has the locale yyyy-MM-dd. check this reference date format by country
set locale to China you'll get the required date format
Try this
String d1="12-27-2010";
Stirng[] splitdata=d1.split("-");
int month=Integer.parseInt(splitdata[0]);
int day=Integer.parseInt(splitdata[1]);
int year=Integer.parseInt(splitdata[2]);
Calender cal=Calender.getInstance(Locale.CHINA);
cal.set(year,month,day);
Date d=cal.getTime();
This should work
If you know your data format you can do that. by using simple string operations
Ex:
if your data format is
MM-dd-yyyy
then you can convert to yyyy-MM-dd like this
String d1="12-27-2010";
Stirng[] splitdata=d1.split("-");
String s2= splitdate[2]+"-"+splitdate[0]+"-"+splitdate[1];
You can use Concatenation Operator(+) for that
Yes possible; just write the code to do the parsing. Should not be that difficult ...
in my App User Selects Date like 2013-01-02
but in MySql DB the column format is like 02-Jan-2013 so want change the Date Format
my code is:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat formatter1 = new SimpleDateFormat("dd-mmm-yyyy");
Date start_date = (Date)formatter.parse(GBRF.getStart_date());//GBRF.getStart_date() returns Form Data as 2013-01-02
String dt=formatter1.format(start_date);
Date sd=(Date)formatter1.parse(dt);
at the End sd print Date like this Wed Jan 02 00:01:00 IST 2013
i dont want that format..
just i want like 02-Jan-2013
give me an idea..
Thanks in Advance
You can format Date into String with SimpleDateFormat, after that if you try to print date instance it will invoke toString() method of Date class which has no change in output and you can't alter that output because it is coming from toString() implmentation
Note: in your format you need to use M for month (note capital M)
A Date object does not have a format in itself. It holds just a point in time. The format comes into play when you convert it to or from a String.
Also note that m in the format is used for minutes not months. So you probably want a format like dd-MMM-yyyy.
Try
System.out.println(formatter1.format(sd));
to get the date printed as you like it.
You need to change your date format first :
`SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MMM-yyyy");`
In your case, if you just print dt, it should print date in the required format instead of converting it into Date object again i.e. sd
When you are printing sd, without any formatting it will print the default String implementation of Date object which is exactly what you are seeing as an output