convert str time to kml timeStamp in java - java

i building a kml file and there is a line of timestamp:
<gx:TimeStamp>
<when>2002-07-09T19:00:00-08:00</when>
</gx:TimeStamp>
i need convert time like: "1430477311" to "2002-07-09T19:00:00-08:00" format
how ?
(java code)
tnx a lot

You are wanting to convert from your timeformat to XML Date Format (ISO-8601) -
long timeStamp = 1430477311L;
java.util.Date yourDate = new java.util.Date(timeStamp*1000); //ms
SimpleDateFormat yyyyMMddTHHmmssSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
String date = yyyyMMddTHHmmssSDF.format(yourDate);
Simple Date Format reference.

Simply pass the timestamp to a Date type:
Timestamp stamp = new Timestamp(inputTimestamp);
Date date = new Date(stamp.getTime());
//change the to the format that you need
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String timeStr = df.format(date);

Related

Formatting TimeZoneDesignator

Need to format this String 2017-08-01T15:43:45+0530 to 2017-08-01T15:43:45+05:30 using a particular date format. Tried with yyyy-MM-dd'T'HH:mm:ssZZ. Did not work..
Date modified = aemPage.getProperties().get(cq:lastModified, Date.class);
private DateFormat seoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZ");
String formattedDate = seoDateFormat.format(modified));
You need three X to get off set like
Sign TwoDigitHours : Minutes
I suggest to use OffsetDateTime if you are working with java8 or higher:
String input = "2017-08-01T15:43:45+0530";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
OffsetDateTime offsetDateTime = OffsetDateTime.parse(input, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(offsetDateTime.format(formatter)); // 2017-08-01T15:43:45+05:30
Since 2017-08-01T15:43:45+05:30 is ISO_OFFSET_DATE_TIME, you can also just use:
String outPut = offsetDateTime.toString();
Update:
If you want to use SimpleDateFormat, try:
String input = "2017-08-01T15:43:45+0530";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(formatter.format(date));
But this only works when your system's offset is 0530 since Date does not hold time zone information.

change date format DD-MON-YYYY to DD/MM/YYYY

I want to change date format which I received reading from excel cell file is "30-mar-2016" to 03/30/2016. I have tried below
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate;
startDate = df.parse(inputDate);
It gave me- java.text.ParseException: Unparseable date: "30-mar-2016"
.
I also tried below code
String inputDate = "30-mar-2016";
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
String startDate;
startDate = df.format(inputDate);
getting -- java.lang.IllegalArgumentException: Cannot format given Object as a Date
Can anyone help me .
Your input string 30-mar-2016 is in the format dd-MMM-yyyy. Your output format is MM/dd/yyyy. So you need two DateForamts. One for parsing original input string, one for formatting output string.
DateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy"); // for parsing input
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy"); // for formatting output
String inputDate = "30-mar-2016";
Date d = df1.parse(inputDate);
String outputDate = df2.format(d); // => "03/30/2016"
I have resolved using below
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse("30-mar-2016");
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));//got 03/30/2016
Thanks everyone for your quick response

How do I change the display of date of birth in java?

In my Java code, date of birth is retrieved from a database in the format 1999-04-30. How can I out.println this in a different format? Like this: 30/04/1999
You can format date as follows,
SimpleDateFormat format = new SimpleDateFormat();
String DateToStr = format.format(yourDataBaseDate);
format = new SimpleDateFormat("dd/MM/yyyy");
DateToStr = format.format(yourDataBaseDate);
System.out.println(DateToStr);
This is sample date format for your
question.
Hopefully, the date is retrieved as a java.sql.Date, which does not have a specific form.
To format such a date value, use:
java.sql.Date birthdate = resultSet.getDate("Birthdate");
SimpleDateFormat dateFmt = new SimpleDateFormat("dd/MM/yyyy");
String text = dateFmt.format(birthdate);
Or, in java.time (Java 8 and later):
java.time.LocalDate birthdate = resultSet.getDate("Birthdate").toLocalDate();
DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern("dd/MM/uuuu");
String text = birthdate.format(dateFmt);

Changing the date format to mm-dd-yyyy hh:mm

I'm trying to use this code:
private static Date getTimeStamp() {
return new Timestamp(new Date().getTime());
}
What I'm getting is 2013-03-13 12:46:22.011
But what I need is that the timestamp should be in the format of mm-dd-yyyy hh:mm.
java.sql.Timestamp objects (just like java.util.Date and java.sql.Date) do not have a format by themselves, so you cannot "have a Timestamp in the format [whatever]".
A format only is applicable when you convert the object to a string for display. You can use SimpleDateFormat to convert a Timestamp to a string, using the format you want.
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm");
String text = dateFormat.format(timestamp);
System.out.println(text);
Hope this helps
String date1="2013-03-13 12:46:22.011";
DateFormat userDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
DateFormat dateFormatNeeded = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = userDateFormat.parse(date1);
String finaldate = dateFormatNeeded.format(date);
You can use SimpleDateFormat http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html to achieve your goal.
For Example:
new SimpleDateFormat("MM-dd-yyyy HH:mm").format(timestamp));

Get time from entire date

I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));

Categories

Resources