I have a ReST service which downloads information about events in a persons calendar...
When it returns the date and time, it returns them as a string
e.g. d
I don't thinks you really need how to split the string, in your case it should be 'how to get time in milliseconds from date string', here is an example:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
String date = "12/8/2012";
String time = "11:25 am";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try {
Date dt = df.parse(date + " " + time);
Calendar ca = Calendar.getInstance();
ca.setTime(dt);
System.out.println(ca.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Use SimpleDateFormat
(check api docs). If you provide proper time pattern it will be able to convert string into Date instantly.
Assuming you get your date in String format (if not, convert it!) and then this:
String date = "12/8/2012";
String[] dateParts = date.split("/");
String day = dateParts[0];
String month = dateParts[1];
Similarly u can split time as well!
You can see an example of split method here : How to split a string in Java
Then simply use the array for your parameter eg: array[0] for year and etc..
This is just a Idea, you can do some thing like this without splitting
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm a");
Date date = formatter.parse("12/8/2012 11:25 am");
Calendar cal=Calendar.getInstance();
cal.setTime(date);
Consider using java.time, the modern Java date and time API, for your date and time work. With java.time it’s straightforward to parse your two strings for date and time individually and then combine date and time into one object using LoalDate.atTime()
.
The way I read your code you are really after a count of milliseconds since the epoch. So this is what I am giving you in the first snippet. Feel free to take it apart and use only the lines you need.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d/M/u");
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("h:mm a")
.toFormatter(Locale.ENGLISH);
String dateString = "12/8/2012";
String timeString = "11:25 am";
LocalDate date = LocalDate.parse(dateString, dateFormatter);
LocalTime time = LocalTime.parse(timeString, timeFormatter);
long startMillis = date
.atTime(time)
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
System.out.println(startMillis);
When running in my time zone (at UTC offset +02:00 in August) the output is:
1344763500000
For anyone reading along that does need the individual numbers from the two strings, getting those is straightforward too. For example:
int year = date.getYear();
Month month = date.getMonth();
int monthNumber = date.getMonthValue();
int dayOfMonth = date.getDayOfMonth();
int hourOfDay = time.getHour();
int hourWithinAmOrPm = time.get(ChronoField.HOUR_OF_AMPM);
int minute = time.getMinute();
System.out.format("Year %d month %s or %d day %d hour %d or %d AM/PM minute %d%n",
year, month, monthNumber, dayOfMonth, hourOfDay, hourWithinAmOrPm, minute);
Year 2012 month AUGUST or 8 day 12 hour 11 or 11 AM/PM minute 25
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).