Android / Java convert String date to long type
Asked Answered
M

2

10

I need to convert a string in the format "dd/mm/yyyy", to a long type. In order to pass the value to the calendarProvider in android.

Currently I have:

Calendar calendar = Calendar.getInstance();
long startEndDate = 0;
Calendar currentDateInfo = Calendar.getInstance();
currentDateInfo.set(calendar.get(Calendar.YEAR), calendar.SEPTEMBER, calendar.get(Calendar.DAY_OF_MONTH));
startEndDate = currentDateInfo.getTimeInMillis();

I need:

long startDate = *Some sort of conversion* EditText.getText();

I've tried using SimpleDateFormat but i'm having problems getting the correct type back. Any help would be much appreciated.

Mayhem answered 30/9, 2014 at 10:57 Comment(1)
Show your attempt of using SimpleDateFormat - it is the correct way to go.Herbert
L
37

You can use the following code to get a long value (milliseconds since January 1, 1970, 00:00:00 GMT) from a String date with the format "dd/mm/yyyy".

try {
    String dateString = "30/09/2014";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date date = sdf.parse(dateString);

    long startDate = date.getTime();

} catch (ParseException e) {
    e.printStackTrace();
}
Leipzig answered 30/9, 2014 at 11:2 Comment(7)
@Ken Wolf Is it possible to combine two separate Strings, one date String and one time String into one String (for example, "30/09/2014" and "10:00 PM" into "30/09/2014 10:00 PM" to get a long value from the combined String? The date String is format "dd/MM/yyyy" and the time String is format "h:mm a".Peggiepeggir
Yes this is possible - you are just combining 2 strings to make 1 string and parsing that. You will have to make sure both strings adhere to the correct formatting.Leipzig
@Ken Wolf Excellent. Are there any conventions like maintain one space between the end of the date string and the beginning of the time string? Would it be best to review SimpleDateFormat examples to see different formats or would you recommend something else for review?Peggiepeggir
This first one here is quite common: fileformat.info/tip/java/simpledateformat.htm I would go with that :)Leipzig
@Ken Wolf Will check it out. I appreciate the quick reply, cheers.Peggiepeggir
@Ken Wolf one more question: is the try/catch block really only used by the coder during the pre-release period for an app? If so, then in production code would the coder remove the try/catch block and put a Toast that is triggered by an error? Something like "There is a problem with the date and time entered. Please correct."Peggiepeggir
No the try catch block stays in production code. You use it to catch exceptions - you can then do whatever you want with them (log locally, log remotely, execute some other code, display a message to the user).Leipzig
P
2

Use like this your code

String[] dateArray = dateString.split("-");

int year = Integer.parseInt(dateArray[0]);
int month = Integer.parseInt(dateArray[1]);
int date = Integer.parseInt(dateArray[2]);

GregorianCalendar gc = new GregorianCalendar(year,month,date);
long timeStamp = gc.getTimeInMillies();
Pilocarpine answered 30/9, 2014 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.