Why won't my SimpleDateFormat convert correctly? - java

I have my Date as String: 2014-06-23 22:00
To get the Date as java.util.date I parse it by using SimpleDateFormat
Date listDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm",Locale.GERMANY);
listDate = sdf.parse(gameList.get(position).getTime());
but my output
System.out.println(listDate)
is 2014-04-118 19:37
What's going on here??

DD should be dd (in small) below are the available formats in Java7

Related

Convert Local time to UTC with timezoneoffset in Java

I have a local date/timestamp and I have a timezone offset stored in String format (myStringTimeStamp variable name below). I need to convert the local date/timestamp to UTC time.
The format the timestamp is in is: yyyy-MM-dd HH:mm:ss
I have tried variations on:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Convert timestamp to date
Date d = sdf.parse(myStringTimeStamp, new ParsePosition(0));
newTimeStamp = sdf.format(d);
But I can't seem to figure out the right formula. Any help?
I can't use 3rd party libraries/frameworks.
Problem seems to be because you're setting the target timezone before parsing.
First parse, then set timezone, and finally format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
//Convert timestamp to date
Date d = sdf.parse(myStringTimeStamp, new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
newTimeStamp = sdf.format(d);
Did you try:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
//Convert timestamp to date
Date d = sdf.parse(myStringTimeStamp, new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
newTimeStamp = sdf.format(d);

The Date(String) is deprecated

My Date constructor is deprecated and highlighted in Yellow.
How can I use Calendar.Set() to resolve this issue. I have called both import java.util.Calendar; and date.
Code is below. Thanks in advance.
Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date d = new Date(f.format(geoState.getString("fireTime")));
temp.setFireTime(d);
Use a DateFormat.parse method to convert a String to Date
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
In your case it will be something like this
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = format.parse(geoState.getString("fireTime"));
temp.setFireTime(date);
I have the same issue while migrating my project to Java 11. Here is the answer
new Date("08/10/2020");
to
DateFormat.getDateInstance().parse("08/10/2020")

Solving Parse Date Exception

i have a string "12/3/2014 12:00:00 AM" and i want to cast this string in date which have format like this "yyyy-MM-dd HH:mm:ss"
#DatabaseField(columnName="out_date",dataType=DataType.DATE_STRING,format="yyyy-MM-dd HH:mm:ss")
private Date out_date;
above is the object to get value from date string
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outlet.setOut_date(formater.parse(json.getString("outletDate")));
after i using like the error occur java.text.ParseException: Unparseable date: ... (at offset 2) how to solve that problem thank in advance
Based on this answer, you need something like this. First convert your string in its present format to a date object, then reformat the date object.
String date = "12/3/2014 12:00:00 AM";
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy HH:MM:SS a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
Conversion steps :
Create date object from your date format.
Target date format provide to SimpleDateFormat class constructor.
Parse date using SimpleDateFormat.
The code provided convert yyyy-MM-dd HH:mm:ss to dd/mm/yyyy HH:mm:ss format.
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/mm/yyyy HH:MM:SS a");
Date date = sdf1.parse("12/3/2014 12:00:00 AM");
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(format);
}catch(Exception e){
e.printStackTrace();
}
Error : java.text.ParseException: Unparseable date: ... (at offset 2) indicates that you are using wrong date format to parse your sting.

How to convert "April 30,2013" into date object?

String str2="April 30, 2013";
simpleDateFormat s= new simpleDateFormat();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date dateObject = null;
I have string like "April 30,2013", I am getting this as string from form. I want to convert this into date object as 04/30/2013. Is there any way to do it?
I tried with simpleDateFormat() but I didn't found proper output.
Use MMMMM dd, yyyy to parse and MM/dd/yyyy to format
Use the following code.
String str2="April 30, 2013";
SimpleDateFormat input = new SimpleDateFormat("MMMM dd, yyyy");
SimpleDateFormat output = new SimpleDateFormat("MM/dd/yyyy");
Date date = input.parse(str2);
System.out.println(output.format(date));
This is how you can convert it String date to Date object:
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d,yyyy");
SimpleDateFormat yourNewDateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse("April 30,2013");
System.out.println(yourNewDateFormat.format(date));

convert string into date format

I want this format 6 Dec 2012 12:10
String time = "2012-12-08 13:39:57 +0000 ";
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);
System.out.println("Time: " + date);
You need to first parse your date string (Use DateFormat#parse() method) to get the Date object using a format that matches the format of date string.
And then format that Date object (Use DateFormat#format() method) using the required format in SimpleDateFormat to get string.
String time = "2012-12-08 13:39:57 +0000";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(time);
String str = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").format(date);
System.out.println(str);
Output: -
08 Dec 2012 19:09:57
Z in the first format is for RFC 822 TimeZone to match +0000 in your date string. See SimpleDateFormat for various other options to be used in your date format.
change SimpleDateFormat to:
String time = "2012-12-08 13:39:57 +0000";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date date = sdf.parse(time);
DateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String formatedTime = sdf.format(date);
System.out.println("Time: " + formatedTime);
Take a look at SimpleDateFormat. The code goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
String reformattedStr = myFormat.format(fromUser.parse(inputString));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yyyy");
Date date=simpleDateFormat.parse("23-09-2008");
You can use the SimpleDateFormat Class to do this!
such:
DateFormat mydate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = mydate1.parse(time);

Categories

Resources