I have the following SimpleDateFormat
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a");
which works fine for the following date
Sunday, November 15, 2015 7:00 PM
The format of the date I'm parsing is not always like that. Sometimes it would be look like below
Saturday, 14 November, 2015 22:04
How can parse both of them successfully?
If you got just two formats this will suffice. If there are more of them you might want to use recursion. If this is the case, please inform me, I'll show you how to do it.
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("EEEE, d MMMM, yyyy H:mm");
Date result = null;
while( logic statement){
try{
result = simpleDateFormat1.parse(dateAsString);
}catch (ParseException e){
result = simpleDateFormat2.parse(dateAsString);
}
//do whatever want with the result.
}
As the OP asked this is the recursion way. You can copy paste it, it runs. If you want Collection of formats instead of an array you may want to use Iterator instead of an index i.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static Date parseDate(String dateAsString, SimpleDateFormat[] formats, int i) {
if (i == formats.length) {
return null;
}
try {
return formats[i].parse(dateAsString);
} catch (ParseException e) {
return parseDate(dateAsString, formats, i + 1);
}
}
public static void main(String[] args) {
SimpleDateFormat[] formats = { new SimpleDateFormat("EEEE, MMMM d, yyyy h:mm a"), new SimpleDateFormat("EEEE, d MMMM, yyyy H:mm"), new SimpleDateFormat("dd.MM.yy") };
String[] datesAsStrings = {"Sunday, November 15, 2015 7:00 PM", "Saturday, 14 November, 2015 22:04", "25.07.15", "this is NOT a date"};
for(String dateAsString :datesAsStrings){
System.out.println(parseDate(dateAsString, formats, 0));
}
}
}
Related
i have done code below to change time from UTC to another time zone but code is showing only UTC time.Also after formatting to source time format it shows system time zone .
private String setTimezone(String time){
sourceformatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
dateFormatter = new SimpleDateFormat("hh:mm a");
sourceformatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.e("reicievedformat",time);
Date value = null;
try {
value = sourceformatter.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("afterfirstformat",dateFormatter.format(value));
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
time =dateFormatter.format(value);
Log.d("Finaltime",time);
return time;
}
Output:- Log values
E/reicievedformat: 12:36 PM, Mon 08 Oct 2018
D/afterfirstformat: 06:21 PM
D/Finaltime: 12:36 PM
As you can see I'm getting 12:36 PM, Mon 08 Oct 2018 ("UTC") and I want to convert to IST, but the final time, 12:36 PM, doesn’t seem to have been converted.
IST in java stands for "Israel Standard Time".
Use this for "Indian Standard Time"
dateFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
The date & time APIs in Java gives you headache.
I use this library by Daniel Lew.
https://github.com/dlew/joda-time-android
Try this
public static String getDateOut(String ourDate) {
try
{
//be sure that passing date has same format as formatter
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a, E dd MMM yyyy");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = formatter.parse(ourDate);
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a"); //this format changeable
dateFormatter.setTimeZone(TimeZone.getTimeZone("IST"));
ourDate = dateFormatter.format(value);
}
catch (Exception e)
{
ourDate = "00-00-0000 00:00";
}
return ourDate;
}
I have a string with six numbers: 650310. It represents 1965 march 10 in YYMMDD format.
Is there any method to recognize this format to 10 march 1965?
Currently this is my method of doing which isn't very effective.
public class Example {
public static void main(String args[]) {
//date in YYMMDD
//String x = "650310";
String x = "161020";
System.out.print(x.substring(4, 6)+" ");
if (Integer.parseInt(x.substring(2, 4)) == 10) {
System.out.print("October"+" ");
}
else if (Integer.parseInt(x.substring(2, 4)) == 03) {
System.out.print("March"+" ");
}
if (Integer.parseInt(x.substring(0, 2)) > 50) {
String yr = "19" + x.substring(0, 2);
System.out.println(yr);
} else if (Integer.parseInt(x.substring(0, 2)) < 50) {
String yr = "20" + x.substring(0, 2);
System.out.println(yr);
}
}
}
output : 20 October 2016
Use Java's SimpleDateFormat:
SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );
Now you have a Date object which you can use to display the date in other formats:
SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );
Use output.toString() to display your newly formatted date. Good luck.
try this example
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String args[]) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat( "yyMMdd" );
Date theDate = s.parse( "650310" );
SimpleDateFormat p = new SimpleDateFormat( "dd MMMMM yyyy" );
System.out.println(p.format(theDate));
}
}
OUTPUT 10 March 1965
This link will help. Create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings.
Use SimpleDateFormat for date parsing. For example:
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
System.out.println(format.parse("900310"));
} catch (ParseException e) {
e.printStackTrace();
}
Output: Sat Mar 10 00:00:00 MSK 1990
EDIT: if you want to parse date,try to use DateFormat to get Date !!!! And then you can format it in your own way. I disagree with your downvote.
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
Date parse = format.parse("900310");
format.applyPattern("dd MMMM yyyy");
System.out.println(format.format(parse));
} catch (ParseException e) {
e.printStackTrace();
}
output 10/Март/1990
I am converting String to Date format. But it returns "Unparseable date". for example,
String date= "Wednesday, May 15, 2013";
I want to convert this to String like "2013-05-15" How to do that?
Use SimpleDateFormat twice: Once to parse a Date, the other to render it in the desired format:
Date date;
String display = new SimpleDateFormat("yyyy-MM-dd").format(
new SimpleDateFormat("EEEE, MMMM dd, yyyy").parse(date)
);
Your example date is unfortunate, because it uses the only 3-letter month "May", so I can't tell if your month names are all truncated to 3 letters, or if they are the full name. I have assumed months to be the full name, but if they are truncated, change MMMM to MMM in the second format string.
Something like this might help (parse the date string to date object and format it back in the new format):
String dateString = "Wednesday, May 15, 2013";
DateFormat format1 = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
Date date = format1.parse(dateString);
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
String updatedDateString = format2.format(date);
System.out.println("Updated Date > "+updatedDateString);
In my experiments with this, you need to do something like the below...Refer to the API for understanding how to construct your format strings. http://docs.oracle.com/javase/6/docs/api/index.html?java/text/DateFormat.html
String myDateAsString = "Wednesday, May 15, 2013";
SimpleDateFormat df = new SimpleDateFormat("EEEE, MMM d, yyyy");
Date d = new Date();
try {
d = df.parse(myDateAsString);
} catch (ParseException e1) {
System.out.println("Could not parse...something wrong....");
e1.printStackTrace();
}
df.applyPattern("yyyy-MM-d");
String convertedDate = df.format(d);
System.out.println(convertedDate);
This will be a good approach.
Something like this:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringDate {
public static void main(String[] args) throws ParseException{
String dateString = "Wednesday, May 15, 2013";
DateFormat format1 = new SimpleDateFormat("E, MMM dd, yyyy");
Date date = format1.parse(dateString);
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
String updatedDateString = format2.format(date);
System.out.println("Updated Date > "+updatedDateString);
}
}
Here's the code that should work, but does not:
public static void main(String[] args) {
String datata = "23:00:01 GMT, Sun Jul 28, 2012";
String format = "HH:mm:ss zzz, EEE MMM dd, yyyy";
try {
DateFormat inputFormat = new SimpleDateFormat(format);
Date parsedDate = inputFormat.parse(datata);
System.out.println(parsedDate.toGMTString());
} catch (Exception e) {
e.printStackTrace();
}
}
I'm getting a parse exception. I triple checked the patterns, I even wrote it one beneath the other, and I still get an exception. Help, anyone?
It is probably because your default locale is not in english and the parser does not understand "Sun" and/or "Jul". Try using:
DateFormat inputFormat = new SimpleDateFormat(format, Locale.ENGLISH);
I 'm getting the date in the following format (Date and not String):
Tue Jun 26 07:00:00 EDT 2012
I want to change the format of the date to (Date):
6/26/2012 10:19:15 AM
so as to update the same in the data base. I tried following code:
Date dte;
Date dte1;(Tue Jun 26 07:00:00 EDT 2012)
SimpleDateFormat formatter = new SimpleDateFormat("m/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte1);
dte = formatter.parse(formattedDate);
SystemUtils.trace("test", " date>>>" + dte);
is yielding the following response:
Thu Jan 26 07:00:00 EST 2012
Can any one please share the piece of code to do the same asap.
You shouldn't have to format dates to insert them in a database. If using JDBC, use prepared statements.
To answer your question, though, m can't mean minute and month at the same time. M means month. m means minute.
This code outputs needed for you result:
Date dte = new Date();//or something else
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte);
System.out.println(formattedDate);
Try this, this below code will suit your need.
public class DateWala {
public static void main(String[] args){
System.out.println(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").format(new Date()));
}
}
you can use this tiny function
// send your time
private String convertTime(String dateTime) {
//source format will go there
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date date = null;
try {
date = sdfSource.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
//destination format will go there
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy HH:mm");
return sdfDestination.format(date);
}