How do I change the display of date of birth in java? - 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);

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.

Generic way to convert a given String date in any format to java.sql.Timestamp

I want to convert a string representation of date in any format to java.sql.Timestamp,(format is also provided)
we have tried using Timestamp.valueOf() and Date.valueOf() but both these method requires String in a particular format,What i need is a generic method which will convert a given String date in any format to java.sql.Timestamp
Thanks in Advance
If you have the string and know the format you can try something like:
dateStr = "25 December 2015"
date = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH).parse(dateStr);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(date.getTimestamp());
Or similar:
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
java.util.Date date = simpleDate.parse("2015-12-31 09:45:52.189");
java.sql.Timestamp = new java.sql.Timestamp(date.getTime());

convert str time to kml timeStamp in 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);

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));

Converting string date to date

How would I convert a String date in the form of 24/04/2012 to a date variable in the format of 24-Apr-12, which can later be passed into my Oracle database.
I have tried this but it says the string date is unparsable:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
I think you are confusing parsing and formatting, try this:
String old_date = "24/04/2012";
DateFormat old_format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = (Date) old_format.parse(old_date);
DateFormat new_format = new SimpleDateFormat("dd-MMM-yy");
String date = new_format.format(newDate);
System.out.println(date);
You are doing this backwards:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = format.parse(date);
DateFormat formatOutput = new SimpleDateFormat("dd-MMM-yyyy");
String output = formatOutput.format(newDate);
But if what you are doing is passing the date to Oracle, you should really use a PreparedStatement
Your date format should be "dd/MM/yyyy"
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy");
DateTime newDate = sdf.format(date);
Try DateFormat format = new SimpleDateFormat("dd/MM/yyyy")
Why don't you try using java.sql.Date.
For example:
Date newDate = new java.sql.Date(date.getTime());
So you can just give an generic sql object for date.
The Date class of Java is quite difficult to use in many cases. I recommend the use of the much better Joda Time classes:
DateTimeFormatter oldFormat = DateTimeFormat.forPattern("dd/mm/yyyy");
DateTimeFormatter newFormat = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = oldFormat.parseDateTime("24/04/2012");
String newDate = newFormat.print(dateTime);

Categories

Resources