Now this one baffles me a lot...
Consider the following 2 dates
Fri Dec 11 00:00:00 CET 2015
Fri Dec 11 23:59:59 CET 2015
When i put both of these dates into SimpleDateFormat i get 2 different results...
Date dateFrom = Fri Dec 11 00:00:00 CET 2015
Date dateTo = Fri Dec 11 23:59:59 CET 2015
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String formatedFromDate = formatter.format(dateFrom);
String formatedToDate = formatter.format(dateTo);
System.out.println(formatedFromDate);
System.out.println(formatedToDate);
OUTPUT:
1st: 11/12/2015
2nd: 11-12-2015
Now if someone could shed some light on what is happening here I would really appreciate it.
PS. I tried creating separate formatter for the other date but still same effect.
EDIT:
Ok so below code is part of css export file.
// os.getStart() returns = Fri Dec 11 00:00:00 CET 2015
// os.getEnd() returns = Fri Dec 11 23:59:59 CET 2015
//
// os.getStart() & os.getEnd() looks like
Date start;
Date end;
// SEPARATE PACKAGE
public Date getStart() {
if(start==null) {
log.warning("Null start!");
start=new Date();
}
return start;
}
public Date getEnd() {
if(end==null) {
log.warning("Null end!");
end=new Date();
}
return end;
}
// setDates are called when date from database is taken - so no rocket science here
// CSV EXPORT FILE
Date dateFrom = os.getStart();
Date dateTo = os.getEnd();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String formatedFromDate = formatter.format(dateFrom);
String formatedToDate = formatter.format(dateTo);
if(names==null) {
return;
}
Iterator it;%><%= title %>,<%= messages.getString("calls") %>,<%= messages.getString("answered") %><%= (hasTimes) ? ","+messages.getString("h_time") : "" %>,From Date,To Date<%
it=breakdown.keySet().iterator();
while(it.hasNext()) {
Integer idInt=(Integer)it.next();
String key=(String)names.get(idInt);
OrderlyStats.Breakdown value=(OrderlyStats.Breakdown)breakdown.get(idInt);
%>
<%= dateFrom %> //<----Fri Dec 11 00:00:00 CET 2015
<%= dateTo %> //<----Fri Dec 11 23:59:59 CET 2015
<%= ("Queue".equals(type)) ? displayNames.get(key) : key %>,<%= value.calls %>,<%= value.answered %><%= (hasTimes) ? ","+Helper.formatDouble(value.time,1) : "" %>, <%= formatedToDate %>,<%= formatedToDate %><% } %>
Your dates are expressed in the CET timezone. Is that your default timezone?
You can specify the target timezone in your date formatter:
formatter.setTimeZone(TimeZone.getTimeZone("CET"));
Related
When I select date in SQL it is returned as:
Wed Jan 31 00:00:00 GMT+01:00 2018
But I need only the Date part, that is Jan 31 2018. How can I do this?
There may be a reason to use org.jdesktop.swingx.JXDatePicker but rather of using JXDatePicker, I will simply show GMT parsing using java.util.Date.
Try this source code:
System.out.println(new Date());
//will show your Date along with your local TimeZone
//result for me is : Sat Jan 13 08:47:59 IST 2018
//First changing local pacific time zone to GMT+01 level explicitly,
//otherwise it will show results as your local time zone by default.
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
String existingDateValue = "Wed Jan 31 00:00:00 GMT+01:00 2018";
DateFormat gmtFormat =
new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
try {
//try to parse and validate existing date
Date validatedExistingDate = gmtFormat.parse(existingDateValue);
System.out.println(validatedExistingDate);
//parsed validated date : Wed Jan 31 00:00:00 GMT+01:00 2018
DateFormat newFormat = new SimpleDateFormat("MMM d, yyyy");
System.out.println(newFormat.format(validatedExistingDate));
//required Date is in GMT format : Jan 31, 2018
} catch (ParseException pex) {
pex.printStackTrace();
} finally {
System.out.println("Current TimeZone : " + new Date());
//now, reverting to my local TimeZone
TimeZone.setDefault(TimeZone.getTimeZone("IST"));
System.out.println("Current TimeZone : " + new Date());
}
9 PM CST 14 NOV 16 or 9:30 PM CST 14 NOV 16
If my string is the date and time above, what should be my date and time pattern that i could use for SimpleDateFormat? Im not sure what should be the pattern if the time could be 9 only or 9:30.
Is this correct?
SimpleDateFormat sdf = new SimpleDateFormat("h:m aaa z d MMM yy");
I tried testing the above code. For sample date 9:30 PM CST 14 NOV 16, it's working. But for 9 PM CST 14 NOV 16, it's throwing an exception:
java.text.ParseException: Unparseable date: "9 PM CST 14 NOV 16"
You could write a method that tries multiple date format patterns:
public static Date parseDate(String dateToParse) {
String[] dateFormats = {"h aaa z d MMM yy", "h:m aaa z d MMM yy"};
for (String dateFormat : dateFormats) {
try {
return new SimpleDateFormat(dateFormat).parse(dateToParse);
}
catch (ParseException e) {
}
}
return null;
}
Then
Date date1 = parseDate("9 PM CST 14 NOV 16");
System.out.println(date1);
Date date2 = parseDate("9:30 PM CST 14 NOV 16");
System.out.println(date2);
outputs
Mon Nov 14 22:00:00 EST 2016
Mon Nov 14 22:30:00 EST 2016
This question already has answers here:
How to get a Date object from String
(5 answers)
Closed 8 years ago.
So I am using DateFormat to convert a date (that I parsed form a CSV) to YYYY-MM-DD format, so I can INSERT it into a MYSQL column which is in DATE format.
I am using DateFormat, but my date output keeps coming out like this:
Mon Dec 29 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Wed Dec 31 00:00:00 CST 2014
How can I convert this date to the format I am looking for?
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.parse(date_parsed[0].toString());
System.out.println("DATE=" + date);
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "Tue Dec 30 00:00:00 CST 2014";
Date date = formatter.parse( dateStr );
formatter.applyPattern("yyyy-MM-dd");
String newDate = formatter.format( date );
System.out.println("DATE=" + newDate);
Output :
DATE=2014-12-30
I have the following:
String[] string_dates = new String[10]
// read in strings formatted via fmt1 dd-MMM-yy)
Date[] date_dates = new Date[10];
for (int i = 0; i < 9; i++){
date_dates[i] = fmt1.parse(string_dates[i]);
What would be the most efficient way to format the Dates in date_dates[] to format dd-MM-yyyy? Should I convert the Strings in strings_dates[] to format dd-MM-yyyy first, and then read them into dates? Thank you.
A Date is the representation of the number of milliseconds since the Unix epoch. It has no concept of a format of it's own (other then that created by toString, which should not worry about)...
Once you have converted the String representation of the date to a Date, you should then use an appropriate formatter to format that date in what ever format you want...
String[] stringDates = {
"01-MAR-2013",
"02-MAR-2013",
"03-MAR-2013",
"04-MAR-2013",
"05-MAR-2013",
"06-MAR-2013",
"07-MAR-2013",
"08-MAR-2013",
"09-MAR-2013",
"10-MAR-2013"};
SimpleDateFormat inFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date[] dates = new Date[stringDates.length];
for (int i = 0; i < stringDates.length; i++) {
try {
dates[i] = inFormat.parse(stringDates[i]);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
SimpleDateFormat outFormat = new SimpleDateFormat("dd-MM-yyyy");
for (Date date : dates) {
System.out.println("[" + date + "] - [" + outFormat.format(date) + "]");
}
Which produces...
[Fri Mar 01 00:00:00 EST 2013] - [01-03-2013]
[Sat Mar 02 00:00:00 EST 2013] - [02-03-2013]
[Sun Mar 03 00:00:00 EST 2013] - [03-03-2013]
[Mon Mar 04 00:00:00 EST 2013] - [04-03-2013]
[Tue Mar 05 00:00:00 EST 2013] - [05-03-2013]
[Wed Mar 06 00:00:00 EST 2013] - [06-03-2013]
[Thu Mar 07 00:00:00 EST 2013] - [07-03-2013]
[Fri Mar 08 00:00:00 EST 2013] - [08-03-2013]
[Sat Mar 09 00:00:00 EST 2013] - [09-03-2013]
[Sun Mar 10 00:00:00 EST 2013] - [10-03-2013]
You should avoid the temptation to save the formatted Date and instead simply keep the Date object and format it as you need.
you can format the string in to date type using following SimpledateFormat in java. Following is the example
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Note
For complete date and time patterns, please refer to this java.text.SimpleDateFormat JavaDoc.
Well in you case please take a look at these resourcesclick here
You can give "dd-MM-yyy". Please try it
This is the String I'm extracting the info from using a regex:
2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message
for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
This is the code I use to extract the String and then try to make it into a Date:
Pattern pattern3 = Pattern.compile(";\\s(.*)");
Matcher matcher3 = pattern3.matcher(s);
matcher3.find();
String t = matcher3.group(1).toString();
try {
Date time = new SimpleDateFormat("dd/MMM/yy hh:mm a").parse(t);
} catch (ParseException e) {
e.printStackTrace();
}
This should be the format of my input:
Thu Oct 25 12:08:25 2012 +0000
And what I want is to make a Date from the aforementioned string which looks like:
25/Oct/12 12:08 PM
But I keep getting these errors:
java.text.ParseException: Unparseable date: "Thu Oct 25 12:08:25 2012 +0000"
Fixed the message for defaulted bonds0null
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)
java.text.ParseException: Unparseable date: "Thu Oct 25 11:52:39 2012 +0000"
at java.text.DateFormat.parse(DateFormat.java:337)
at GitItem.cultivateGitItem(GitItem.java:42)
at main.main(main.java:9)
Your pattern has to match the pattern of the incoming data, which it doesn't right now.
SimpleDataFormat can't read your mind, the pattern you are giving it doesn't match the format you are passing into .parse().
"dd/MMM/yy hh:mm a" will never match Thu Oct 25 12:08:25 2012 +0000, you have to specify the exact pattern that the incoming data is in, this is very well documented in the JavaDocs.
Then you can change the pattern to what you want using .applyPattern() can call .format() to get the formatted output you want.
I would simply remove the unwanted part:
String dateAsString = s.replaceAll(".*;\\s+","");
Then you need to DateFormat: one to parse the string and another one to output the correct format:
String s = "2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000";
System.out.println("s = " + s);
String dateAsString = s.replaceAll(".*;\\s+","");
System.out.println("dateAsString = " + dateAsString);
DateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy X", Locale.ENGLISH);
Date date = parser.parse(dateAsString);
System.out.println("date = " + date);
DateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy hh:mm a", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formatter.format(date));
outputs:
s = 2823893a2f91c7507831f140dd7aa75e420477b0 - #0023922: Fixed the message for defaulted bonds ; Thu Oct 25 12:08:25 2012 +0000
dateAsString = Thu Oct 25 12:08:25 2012 +0000
date = Thu Oct 25 14:08:25 CEST 2012
25/Oct/2012 12:08 PM
Note: you need to use the appropriate locale to parse and print the month/day names