Android: SimpleDateFormat prints duplicates across multiple fragments - java

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

Related

How to select only date with JXDatepicker

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());
}

getDateTimeInstance customized time format

Using this code:
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, new Locale("no")).format(Date);
Date 3 AM CST 12 DEC 16 would have a result of: 12. desember 2016 kl 03.00 CST
But what if I only want to show the hours in the time format and remove the minutes and seconds if ever it was present on other locales?
Expecting a result of 12. desember 2016 kl 03 CST or if using English locale then should only be December 12, 2016 3 AM CST
How about having a SimpleDateFormatter like below:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MMMMM yyyy aa hh z", new Locale("no"));
System.out.println(dateFormat.format(new Date()));

SimpleDateFormat date and time pattern

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

Parsing the date, and converting to DATE for MYSQL? [duplicate]

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

Java String to Date unparsable date

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

Categories

Resources