How to GMT date convert into MM/dd/yyyy using java - java

Here, I attached my code and finally I get the date in string format (ex: 1). But I want it for date format (ex: 2)
Ex:1
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Example{
public static void main(String []args){
java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2017");
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formatz = formatter.format(date);
System.out.println(formatz);
}}
The O/P is:
12/01/2017 // String so i cant able to parse it on my query.
Ex:2
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Example{
public static void main(String []args){
java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2017");
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formatz = formatter.format(date);
Date d1 = formatter.parse(formatz);
System.out.println(d1);
}}
The O/P is:
Sat Dec 01 00:00:00 GMT 2017 // but i want 12/01/2017 (date format)
how can i resolve it.

try this:
System.out.println(formatter.format(d1));

First of all, I cannot more strongly advise against using the legacy java.util.Date and java.util.Calendar. You should always use the appropriate class in the java.time package instead.
Now, onto your question. Neither java.util.Date, nor any of the java.time.* classes, contain information about formatting. They contain simply enough information to represent the moment in time. If you want to use a different format from the default, you will need to use either a built-in formatter, or your own.
In your case, because formatz already has the format you want, you can simply print that instead.

Related

Java DateFormat issue on Mac vs Windows

I am using the code below on Mac OSX 10.10.2 and it's behaving strangely.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args) throws Exception {
String dateInString = "23/Oct/2015";
DateFormat formatter = new SimpleDateFormat("dd/MMM/YYYY");
Date date = formatter.parse(dateInString);
System.out.println(date);
}
}
Output on Mac: Sun Dec 28 00:00:00 CST 2014
Output on Windows: Fri Oct 23 00:00:00 CDT 2015
Why is the Mac output wrong?
Y is for the week year. Use y for the year.
DateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Also, make sure the DateFormat's locale is the right one.
EDIT:
A Date has millisecond precision, so if you want nanosecond precision, you shouldn't use Date and SimpleDateFormat.
S is for milliseconds. Since you tell SimpleDateFormat that the last part of the string is milliseconds, it parses it as that: 545000000 milliseconds (i.e. a bit more than 6 days, which explains the difference between the input and the output).
To get an accurate result, to the millisecond, remove the last 6 characters of the string, and use the pattern "dd-MMM-yyyy-HH.mm.ss.SSS".

output need to be in date format

is this possible to get the output in Date type in the below format
> 2014-11-12 09:23:47 GMT+05:30
not to be like
> Wed Nov 12 06:53:47 IST 2014
That can be done using SimpleDateFormat with the format string:
yyyy-MM-dd HH:mm:ss 'GMT'XXX
as per the following program:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date dt1 = new Date();
DateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss 'GMT'XXX");
String line = df.format(dt1);
System.out.println(line);
}
}
On my system, that gives me:
2014-11-14 15:36:16 GMT+08:00

Troubleshooting Java DateFormat Month Parsing Issue

I am running into some odd output using the Java, DateFormat object. For some reason it is adding one to my month and I am not sure why. I have broken down the problem as simple as possible.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* #author djc39_000
*/
public class TestDate {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date testDate;
try {
testDate = (Date) formatter.parse("12/6/2013 11:23:04 AM");
System.out.println(testDate);
} catch (ParseException ex) {
System.out.println(ex);
}
}
}
Output:
Sun Jan 06 11:23:04 EST 2013
Expecting Output:
Fri Dec 06 11:23:04 EST 2013
Also, if I change the month to 11 in my string it does not change the month in the stamp. What am I doing wrong? Thanks
Solution was found, mm is for mins, and I used for month which should have been MM.
Are there any other characters that might be easily confused for bonus points?
Date format should be "MM/dd/yyyy hh:mm:ss a"

Mysql Date Format in Java

I have a website which supplies date in 2 formats: 28th June 2009 or June 2009.
Now I would like to convert both of these into the same format yyyy-mm-dd hh:mm:ss using MySQL and Java.
SimpleDateFormat gives an error: "Unparsable Date". What's the solution?
What about June 2009 as you can not say its a date you need to make it a date by adding a day in this month-year format. Ex.. add first day of month here and make it 1 June 2009 then parse it in desired format.
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException, ParseException
{
String dateStr = "28 June 2009";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(new Date(dateStr)));
}
}

DataTypeConvertor parseDateTime result

I am doing something like this in my program :
Calendar cal = DatatypeConverter.parseDateTime("2012-05-29T11:17:04.805-07:00");
System.out.println(cal.getTime().toString());
o/p:
Tue May 29 13:17:04 CDT 2012
Why is the result showing time of 13:17:04, in the input I have given 11:17:04 and time zone -07:00 which is pacific time zone. Should it not print out 11:17:04 ?
Your timezone - the default one when the program is running is different from the timezone given to the DatatypeConverter.parseDateTime() method and the cal.getTime().toString() method used the default timezone to format the date.
Never use Date.toString() to format Date - a Date only knows the milliseconds from the Epoch time. Instead use java.text.SimpleDateFormat like this:
SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(dateObject).
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import javax.xml.bind.DatatypeConverter;
class TestDate
{
public static void main(String[] args)
{
Calendar cal = DatatypeConverter.parseDateTime("2012-05-29T11:17:04.805-07:00");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
df.setTimeZone(TimeZone.getTimeZone("GMT-07:00"));
String date = df.format(cal.getTime());
System.out.println(date);
}
}

Categories

Resources