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
Related
The structure of my application is a FragmentTabHost which contains a Fragment, inside thatFragment is a ViewPager which allows the user to scroll through child Fragments.
(FragmentTabHost -> Fragment -> ViewPager -> Fragment)
Within the final Fragment, the date of the data it is processing should be displayed.
Here is the code I am using to create the date:
String format = "MMMM F";
SimpleDateFormat formatter = new SimpleDateFormat(format);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString = (formatter.format(item.getDate("date")) + suffixForDayInDate(item.getDate("date")));
Log.d("RAW DATE", item.getDate("date").toString());
Log.d("AFTER FORMATTING", formatter.format(item.getDate("date")));
Output:
D/RAW DATE: Mon Mar 07 17:00:00 MST 2016
D/AFTER FORMATTING: March 2
D/RAW DATE: Sun Mar 06 17:00:00 MST 2016
D/AFTER FORMATTING: March 1
D/RAW DATE: Sat Mar 05 17:00:00 MST 2016
D/AFTER FORMATTING: March 1
D/RAW DATE: Fri Mar 04 17:00:00 MST 2016
D/AFTER FORMATTING: March 1
Please tell me someone has a clue here.
In the below line, you are using the incorrect Format to parse your Date. It should be dd instead of F.
String format = "MMMM F"; /* Correction Required */
From JAVA Docs:
F Day of week in month
d Day in month
You should correct it as follows:
String format = "MMMM dd";
Here is the sample code snippet:
public static void main (String[] args)
{
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
parser.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dt = parser.parse("Mon Mar 07 17:00:00 MST 2016");
SimpleDateFormat formatter = new SimpleDateFormat("MMMM dd");
String date = formatter.format(dt);
System.out.println(date);
}
Input:
Mon Mar 07 17:00:00 MST 2016
Output:
March 08
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"));
I have to convert the date Sun Oct 25 18:41:35 IST 2015 into Oct 25 2015 in java.
Any one can you please suggest?
Use this
java.util.Date date = new Date("Sun Oct 25 18:41:35 IST 2015");
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
String format = formatter.format(date);
System.out.println(format);
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 want to schedule a daily job at 23:59:59 only in weekdays (monday - friday).
i use this cron expression
"59 59 23 ? * MON-FRI",
but the output has tripe value for monday
Wed Aug 29 23:59:59 ICT 2012
Thu Aug 30 23:59:59 ICT 2012
Fri Aug 31 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Tue Sep 04 23:59:59 ICT 2012
Wed Sep 05 23:59:59 ICT 2012
Thu Sep 06 23:59:59 ICT 2012
Fri Sep 07 23:59:59 ICT 2012
is the expression wrong? need help.
i'm getting this output by loop through specific date, here the code
`try {
CronExpression ce = new CronExpression(59 59 23 ? * MON-FRI);
Calendar start = Calendar.getInstance();
start.setTime(new Date());
Calendar end = Calendar.getInstance();
Date endDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse("Fri Sep 29 23:59:59 ICT 2012");
end.setTime(endDate);
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
} catch (ParseException ex) {
Logger.getLogger(HelloJob.class.getName()).log(Level.SEVERE, null, ex);
}
}`
The problem isn't in you rule or in Quartz, it's OK and you may use it.
The problem is in your test code.
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
You're not iterating on valid dates but on all days between startDate and endDate.
The loop content is called for invalid days too and for each of those 2 invalid days the "next valid time" after current date is monday. So you have thrice monday, that's perfectly logic.
Hence your log.