The following time formats are in outlook calendar file
DTSTART;TZID="Eastern":20100728T140000
DTEND;TZID="Eastern":20100728T150000
how to convert this time to java time format.
This looks like iCalendar. Take a look at ical4j - a Java API for it.
Without tested, look at SimpleDateFormat
String ds = "DTSTART;TZID=\"Eastern\":20100728T140000";
Date d = new SimpleDateFormat("yyyyMMdd'T'HHMMSS").parse(ds.split(":")[1]);
Handling the timezone will be tricky as "Eastern" is not an actual timezone. However if you handle that, I would suggest the following SimpleDateFormat will handle the unadjusted parse for you.
Date unadjusted =
new SimpleDateFormat("yyyyMMdd'T'HHmmss").parse(line.split(":")[1]);
Another way using always SimpleDateFormat:
String[] strings = new String[]{"DTSTART;TZID=\"Eastern\":20100728T140000", "DTEND;TZID=\"Eastern\":20100728T150000"};
for (String string : strings) {
String dateString = string.replaceAll("(DTSTART|DTEND);TZID=\"Eastern\":", "");
dateString = dateString.replaceAll("T", "");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date date = sdf.parse(dateString);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
Related
I need to convert a time stamp that currently is in string format "08.00" to a valid time in java so I later can compare time. How do I convert this string to time?
Something like this
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date date = sdf.parse(time);
Instead of using the Date and/or SimpleDateFormat classes, perhaps consider LocalTime
String time = "08:00";
LocalTime lt = LocalTime.parse(time);
System.out.println(lt);
Output:
08:00
And can compare to other times easily with LocalTime::isBefore() or LocalTime::isAfter()
Example
Try below code,
String time = "08.00";
try {
DateFormat sdfInput = new SimpleDateFormat("hh.mm");
Date date = sdfInput.parse(time);
DateFormat sdfOutput = new SimpleDateFormat("hh:mm");
Log.e( "Time: ", sdfOutput.format(date));
} catch (Exception e) {
e.printStackTrace();
}
Output -> Time: 08:00
The easiest way to do so is with a SimplDateFormatter:
DateFormat sdf = new SimpleDateFormat("hh:mm")
Then you can call Date date = sdf.parse(*your time string*)
Now you have your time as a valid Date object.
I have timeformat like this hhmmss="151918"
so you can use any format instead of hhmmss according to your current time format like
"hh.mm" or hh:mm:ss etc
and you can call this method form any where you needed.
fun convertTimeFormat(time:String):String{
var formattedTime=""
try {
val inputFormat: DateFormat = SimpleDateFormat("hhmmss")
val timeObj = inputFormat.parse(time)
Log.d("timeObj",""+timeObj)
formattedTime=SimpleDateFormat("hh:mm aa").format(timeObj)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedTime
}
Hi can anyone help please? I am trying to format a date and time string.
Currently it looks like this "20160112T110000Z" and I need it to be "2016-01-12T11:00:00Z"
The string without the special characters are returned from a 3rd party recurrence library. I need to convert it to have the special characters before parsing it to a Calendar object.
Can anyone help please?
The code that I have so far looks like:
final String TIMEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
String string = "20160112T110000Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = format.parse(string);
System.out.println(date);
However this just does not work.
Any suggestions are appreciated
You have to read the string with a format matching the source, this gives you a correct Date.
Then simply write it with the format you want :
String string = "20160112T110000Z";
String originalStringFormat = "yyyyMMdd'T'HHmmss'Z'";
String desiredStringFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat readingFormat = new SimpleDateFormat(originalStringFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(desiredStringFormat);
try {
Date date = readingFormat.parse(string);
System.out.println(outputFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
try this
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// you can change format of date
Date date = formatter.parse(strDate);
Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;
I have a string "1427241600000" and I want it converted to "yyyy-MM-dd" format.
I have tried, but I am not able to parse it, please review the below code
try {
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
I would like to know where I went wrong.
You should try it the other way around. First get the Date out of the milliTime and then format it.
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.parseLong(str));
System.out.println(sf.format(date));
the conversion is highly dependent on what format the timestamp is in. But i assume the whole thing should actually be a long and is simply the systemtime from when the timestamp was created. So this should work:
String str = ...;
Date date = new Date(Long.parseLong(str));
Use Date date =new Date(Long.parseLong(str)); to convert your String to Date object.
if you are using SimpleDateFormat() the format specified as a parameter to this function should match the format of the date in the String (str in your case). In your case yyyy-MM-dd does not match the format of the time stamp (1427241600000).
You can do it like this:
use a SimpleDateFormat with an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");
In my code, I've to convert a String value(Input) to java.sql.Date format. The problem I am facing is , the input date format is undefined, and it could be in any format. For example , input string may be "Jan 10,2014" or "01-10-2014" or "2014/Jan/10". So now I need to convert this input value into java.sql.Date(DD/MMMM/YYYY). Is there any way to do this conversion?
That is not possible.
You cannot differentiate dd/MM/yyyy and MM/dd/yyyy.
You really need to know the format otherwise your program will probably not behave the way you want.
Try using a List of all the patterns mentioned above using SimpledateFormat.
Something like this:
SimpleDateFormat format1 = new SimpleDateFormat("MMM dd,yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat format3 = new SimpleDateFormat("yyyy/MMM/dd");
// Note: be sure about the format, or else you may end up assigning incorrect values
List<DateFormat> list = new ArrayList<DateFormat>();
list.add(format1);
list.add(format2);
list.add(format3);
for (DateFormat format : list) {
try {
System.out.println(format.parse("Jan 10,2014"));
// Match found. Take action
} catch (ParseException exception) {
// Ignore. Try other pattern
}
}
In PHP to convert a string to DateTime() its very very easy:
$dateTime = new DateTime("2013-12-11 10:109:08");
echo $dateTime->format("d/m/Y"); // output 11/12/2013
What is the equivalent in Java? I've seen a lot of questions in stackoverflow. I cant find a way to solve this problem.
My last try is:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ITALIAN);
return dateFormat.format(new Date(datetime)).toString();
This crash application. Android Studio tells me that Date(java.lang.String) is deprecated.
Can someone help me?
// First convert the String to a Date
String dateTime = "2013-11-12 13:14:15";
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALIAN);
Date date = dateParser.parse(dateTime);
// Then convert the Date to a String, formatted as you dd/MM/yyyy
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormatter.format(date));
You can let the parser / formatter take the timezone into account by using SimpleDateFromat.setTimeZone() if you have to deal with TimeZones that are not in your default locale.
try this
String time1="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
calendar.setTimeInMillis(yourmilliseconds);
time1=sdf.format(calendar.getTime());
Yes As of JDK version 1.1, Date(java.lang.String) is deprecated and replaced by DateFormat.parse(String s).
Parse it like that:
SimpleDateFormat formatter =
new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY);
Calendar date = Calendar.getInstance();
try {
date.setTime(formatter.parse("12.12.2010"));
} catch (ParseException e) {
e.printStackTrace();
}
Have a look at my Android date picker example here.