Parsing Date that is read from file - java

I have record in the file as 17 Dec 2010 17:02:24 17 Dec 2010 18:02:24. I am reading these from file....
my parser code is:
static SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
public static String DateFormat(String startdate) {
String date = null;
try {
java.util.Date tDate = df.parse(startdate);
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String formatteddate = df.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}
but only first date format get parsed...then error will be unable to parse

you are over writing the df value again with a different format (as shown below) in the DateFormat(...) method. df is a static variable so it will use this new format for sub sequent reads. Use a new local variable for "dd-MMM-yy hh:mm:ss a"
df = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");

I hope this helps.
static SimpleDateFormat inputDateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
static SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
public static String getFormattedDate(String startdate) {
String date = null;
try {
java.util.Date tDate = inputDateFormat.parse(startdate);
String formatteddate = outputDateFormat.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}

Your problem is that you're re-using df as Pangea stated.
static SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
public static String DateFormat(String startdate) {
String date = null;
try {
java.util.Date tDate = df.parse(startdate);
SimpleDateFormat outputDf = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
String formatteddate = outputDf.format(tDate).toUpperCase();
return formatteddate;
} catch (ParseException e) {
System.out.println("Unable to Parse" + e);
}
return date;
}

Related

Date parsing yyyy-MM-dd'T'HH:mm:ss in Android

My string date --> 2016-10-02T00:00:00.000Z. I want to get only date from this string. I tried to parse through below coding but it throws me error! I have exactly the same format as mentioned in the string. Any answers?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
try {
Date myDate = sdf.parse( dateofJoining.replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
System.out.println("Date only"+ myDate );
} catch (ParseException e) {
e.printStackTrace();
}
I also tired below code,
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error which i get
java.text.ParseException: Unparseable date: "2016-10-02T00:00:00.000Z" (at offset 19)
05-12 00:18:36.613 4330-4330/com.vroom.riderb2b W/System.err: at java.text.DateFormat.parse
change the simple date format to use: yyyy-MM-dd'T'HH:mm:ss.SSSZ
in your code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date date = format.parse(dtStart.replaceAll("Z$", "+0000"));
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you want to get date/mm/yy from it:
use:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
// use UTC as timezone
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Log.i("DATE", sdf.format(date)); //previous date object parsed
if you want output format: hour:minute AM/PM
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(date));
EDIT
More easier option is to split the string in two parts like:
String dateString = "2016-10-02T00:00:00.000Z";
String[] separated = dateString.split("T");
separated[0]; // this will contain "2016-10-02"
separated[1]; // this will contain "00:00:00.000Z"
try with this :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Try
Calendar calendar = Calendar.getInstance();
TimeZone localTZ = calendar.getTimeZone();
String format1 = "yyyy-MM-dd"; //will return 2017-01-31
String format2 = "dd"; //will return DAY only like 31
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(localTZ );
String result = sdf.format(your_date);
For me it's worked like this:
textView_last_comm.setText(parseDateFormat(passDetailsModel.getLast_comm(), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "dd/MM/yy HH:mm"));
public static String parseDateFormat(String dateToFormat, String inputFormat, String outputFormat) {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormat);
Date date = null;
String str = null;
try {
date = inputFormat.parse(dateToFormat);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
You can get date easily by using String.substring() method:
String string = "2016-10-02T00:00:00.000Z";
String date = string.substring(0, 10);
Log.d("SUCCESS", "DATE: " + date);
OUTPUT:
D/SUCCESS: DATE: 2016-10-02

Date format in android development

I have been trying to format a date from a string but is not working for me. This is my could anyone please check whether there is something wrong with it. Thank you
String edate = "23/04/2016";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, d MMM yyyy ", Locale.UK);
try {
Date trydate = dateFormat.parse(edate);
System.out.println("Try "+ trydate);
} catch (ParseException e) {
e.printStackTrace();
}
j
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); // here set the pattern as you date in string was containing like date/month/year
Date date = dateFormat.parse(edate);
} catch (ParseException ex) {
//Be sure to catch the exception!
}
In this format you wouldnt be getting time though>
Look the API :SimpleDateFormat(String pattern, Locale locale) ;
May be the locale cann't parse the edate that you give, if you change the value of edate like "Fri, 02-Jan-2020 00:00:00 GMT",it will works well;
Here is the code:
public class DateFormat {
public static void main(String[] args) {
String dateStr = "Fri, 02-Jan-2020 00:00:00 GMT";
String pattern = "EEE, dd-MMM-yyyy HH:mm:ss z";
SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
try {
Date t=format.parse(dateStr);
System.err.println(t);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Convert String to date, Throws ParseException

I want to convert the string 11-7-2013 10:51:10 to a Date object.
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
I have the following code, But I'm getting a ParseException.
Try
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Use
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
In SimpleDateFormat the string passed to constructor is the same representation of date format that date is going to parse by this formatter.
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
instanse of
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
You need to use "-" instead of "/" in your date format as your date string contains "-" ,
formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
try {
String date = "11-7-2013 10:51:10"
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
There are two ways here
"dd/MM/yyyy hh:mm:ss" with String Date ="11/7/2013 10:51:10";
or
"dd-MM-yyyy hh:mm:ss" with String Date="11-7-2013 10:51:10";
please refer the below format
tring string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);

how to convert a date dd / mm / yyyy to yyyy-MM-dd HH:mm:ss Android [duplicate]

This question already has answers here:
What's the best way to parse an XML dateTime in Java?
(9 answers)
Closed 6 years ago.
How can I convert a date in dd / mm / yyyy to a format that supports sqlite yyyy-MM-dd'T'HH: mm: ss
for example:
public static String convertStringToData(String stringData)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/aaaa");//yyyy-MM-dd'T'HH:mm:ss
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date data = sdf.parse(stringData);
String formattedTime = output.format(data);
return formattedTime;
}
public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {
Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
String parsedDate = formatter.format(initDate);
return parsedDate;
}
This will return the parsed date as a String, with the format (both initial and end) as parameters to the method.
SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy MM dd HH:mm:ss" );
Date date;
try {
date = originalFormat.parse("21 6 2013");
System.out.println("Old Format : " + originalFormat.format(date));
System.out.println("New Format : " + targetFormat.format(date));
} catch (ParseException ex) {
// Handle Exception.
}
Old Format : 21 06 2013
New Format : 2013 06 21 00:00:00
Date initDate = new SimpleDateFormat("dd/MM/yyyy").parse("10/12/2016");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String parsedDate = formatter.format(initDate);
System.out.println(parsedDate);
public static final String DATE_DASH_FORMAT = "yyyy-MM-dd";
public static final String DATE_FORMAT = "MM/dd/yyyy";
public static String prepareYearMonthDateFromString( String dateStr ){
try
{
Date date = new SimpleDateFormat( DATE_FORMAT , Locale.ENGLISH ).parse( dateStr );
DateFormat formatter = new SimpleDateFormat( DATE_DASH_FORMAT , Locale.getDefault() );
dateStr = formatter.format( date.getTime() );
}
catch( ParseException e )
{
e.printStackTrace();
}
return dateStr;
}

Convert EST time to local Time in java

I'm unable to convert (12/19/2012 8:57am EST) to local Time (now Indian Time).
While converting I'm getting wrong time (Dec 19 2012 11:27). I'm using the following code:
private void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
TimeZone timeZone = TimeZone.getDefault();
sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
}
You dont need to set the timeZone as the the time zone is already specified in the pubDate string. When you want to format using different SDF, the default timezone will convert it into default timezone itself. For eg. if you are in india, IST time = Dec 19 2012 19:27
private static void convertEdtToLocalTime(String pubDate)
{
//pubDate = 12/19/2012 8:57am EST;
String localPubDate=null;
try
{
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy HH:mma z");
// TimeZone timeZone = TimeZone.getDefault(); // No need to do it
// sdf.setTimeZone(timeZone);
if (pubDate != null)
{
Date date = sdf.parse(pubDate);
sdf = new SimpleDateFormat("MMM dd yyyy HH:mm");
localPubDate = sdf.format(date);
}
}
catch (ParseException e)
{
}
System.out.println(localPubDate);
}

Categories

Resources