SimpleDateFormat and Date object [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Month name as a string
(12 answers)
Closed 6 years ago.
In my android app I retrieve from an api the current day as.
long dateTime = innerJSON.getLong("dt");
Date weekDay = new Date(dateTime * 1000L);
SimpleDateFormat outFormat = new
SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
And I get the current day ie Monday.
For the date I use the same Date object but with a different SimpleDateFormat object.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd-M-yyyy");
String date = outFormat1.format(weekDay);
And that gives me,
28-5-2016
which is fine. However, I want the date to have a format of
28 May
Any ideas on that? I checked the official orarcle's site,but I can't find a solution.
Thanks,
Theo.

Try this
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
you will get the output you required.

You need to use dd MMM format here.
More details provided here in official documentation.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
String date = outFormat1.format(weekDay);

haha. That was easy.
SimpleDateFormat("dd MMM");
It's summer now and sunny,so I can't concetrate:):)

Related

Issue with converting String to Date [duplicate]

This question already has answers here:
Comparing time is incorrect when picking 12:00
(3 answers)
Closed 4 years ago.
I tried to convert a String to Date by the following way in Java:
public static Date dateConv1(String s){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
String dateInString = s;
Date date = new Date();
It converts the noon 12:00-12:59 as midnight i.e 00:00-00:59. Could soeone let me know how I should solve this issue?
Change your formatter:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HH - means hours
Reach to: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and see all the examples you need.
The String is not correctly formated. hh means 1-12 hours. There are 4 way to format hours (hh, HH, kk, KK).
Here you can read about the formatting:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
" hh " is for 12 hours format change it to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java - Convert date string to UTC time [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}

Represent MM-DD-YYYY as Day, Month Date [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I want to represent date in the format MM-DD-YYYY as Day, Month Date
For example - represent 5-12-2015 as Tuesday, May 12. How can I do this?
Though I am giving you the code to do this, you should first show us your efforts.
String date = "5-12-2015";
DateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Date d = format.parse(date);
DateFormat format1 = new SimpleDateFormat("EEEE, MMM dd");
String finalDateString = format1.format(d);
System.out.println(finalDateString);

Convert date from m/d/yy to mm/dd/yyyy [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Conversion of Date
How to convert date from m/d/yy to mm/dd/yyyy?
I get the date in a String as: 5/1/12.
I need to convert it to 05/01/2012.
Please suggest.
I used the following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
d_date = dateFormat.parse("5/1/12");
strDate = dateFormat.format(d_date);
Result:
05/01/0012
Expected Result:
05/01/2012
Thanks
Prasad
Try the following:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);
The following links should prove helpful for more complicated tasks:
Oracle SimpleDateFormat Docs
Example code

Date Conversion from String to sql Date in Java giving different output? [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 3 years ago.
I have a string form of Date. I have to change it to Sql Date. so for that i have used the following code.
String startDate="01-02-2013";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
when i used the above code and run that. I got the following output.
2013-01-01.
Here Month is not converted correctly.
Please tell me where is the problem and provide sample code to get correct result?
mm is minutes. You want MM for months:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
Don't feel bad - this exact mistake comes up a lot.
mm stands for "minutes". Use MM instead:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
You need to use MM as mm stands for minutes.
There are two ways of producing month pattern.
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); //outputs month in numeric way, 2013-02-01
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); // Outputs months as follows, 2013-Feb-01
Full coding snippet:
String startDate="01-Feb-2013"; // Input String
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); // New Pattern
java.util.Date date = sdf1.parse(startDate); // Returns a Date format object with the pattern
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
System.out.println(sqlStartDate); // Outputs : 2013-02-01
That is the simple way of converting string into util date and sql date
String startDate="12-31-2014";
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
While using the date formats, you may want to keep in mind to always use MM for months and mm for minutes. That should resolve your problem.

Categories