getDateTimeInstance customized time format - java

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

Related

Android: SimpleDateFormat prints duplicates across multiple fragments

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

DateFormat giving incorrect value

I wanted to convert String to Date. My code:
String maturityDate = "20150722";
SimpleDateFormat formatter = new SimpleDateFormat("yyyymmdd");
Date date = formatter.parse(maturityDate);
System.out.println(date);
Expected Result :
Input 20150722
Output Wed Jul 22 00:07:00 IST 2015
Actual Result :
Input 20150722
Output Thu Jan 22 00:07:00 IST 2015
What could be the cause?
What could be the cause?
Cause is m letter means minute in SimpleDateFormat pattern. You mean M for months.
SOLUTION
Change your format from yyyymmdd to yyyyMMdd.
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
In the API you will find the complete list:
M Month in year Month July; Jul; 07
m Minute in hour Number 30

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

wrong date in SimpleDateTime parse

This is the string that I have:
Sat, Nov 02, 2013 at 5:10 pm
I'm trying to parse it into a date time using this formatter:
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, YYYY 'at' K:mm a");
However, this is what it returns when I use it to parse the date string:
Sat Jan 05 17:10:00 CST 2013
I assume I'm getting the formatter wrong, but I can't figure out where.
Capital YYYY is the format for something called the "week year". You want the lowercase yyyy for the actual year.
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, yyyy 'at' K:mm a");
With this change I output the parsed date and get:
Sat Nov 02 17:10:00 PDT 2013
(I'm in the Pacific time zone.)

java date format - GMT 0700 (PDT)

This is the date format that I need to deal with
Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)
But I don't get what the last two parts are. Is the GMT-0700 fixed? Should it be something like this?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT-0700' (z)");
No, it is not fixed. It is a TimeZone. You can match it with Z in the date format.
To be more precise, in SimpleDateFormat formats :
Z matches the -0700 part.
GMT is fixed. Escape it with some quotes.
z matches the PDT part. (PDT = Pacific Daylight Time).
The parenthesis around PDT are fixed. Escape them with parenthesis.
You can parse your date with the following format :
EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'
Another remark : Wed Aug contains the day and month in English so you must use an english locale with your SimpleDateFormat or the translation will fail.
new SimpleDateFormat("*format*", Locale.ENGLISH);
Here is the Javadoc:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
For this example: Wed Aug 21 2013 00:00:00 GMT-0700 (PDT), you'd want this format:
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaDate {
public static void main (String[] args) throws Exception {
String s= "Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)";
SimpleDateFormat sdf =
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('Z')'");
Date d = sdf.parse (s);
System.out.println ("Date=" + d + "...");
}
}
EXAMPLE OUTPUT: Date=Tue Aug 20 23:00:00 PDT 2013...
Thanx to Arnaud Denoyelle above for his edits!

Categories

Resources