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

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

Related

How to format the given date "31.12.9999" in the format "yyyy/MM/dd HH:mm:ss"

i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,
String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
Please suggest how to achieve this.
Please try below i mentioned date format code,I hope it will help you,
From this,
String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);
To Change:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate :: " + formattedDate);
Thanks,.

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

Java - SimpleDateFormat parse from String issue

I'm trying to parse the following string to a Date object:
String str = "04/15/2014 10:30:24"
I'm using SimpleDateFormat :
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date orderDate = sdf.parse(str);
java.sql.Date orderSqlDate = new java.sql.Date(orderDate.getTime());
but orderSqlDate always returned: 04/15/2014 00:00:00
how to use SimpleDateFormat in java exactly?
The java.sql.Date javadoc states
To conform with the definition of SQL DATE, the millisecond values
wrapped by a java.sql.Date instance must be 'normalized' by setting
the hours, minutes, seconds, and milliseconds to zero in the
particular time zone with which the instance is associated.
If you're going to use java.sql.Date, there's no way around this.
You are also doing correct.
But to get the result in the format you want, you need to use .format("/your format/") method after parsing the string.
String date = "15/12/2014 10:42:24";
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date parseDate = dateParser.parse(date);
formatter.format(parseDate) // this will change format of date as you want.
I don't think the way you parse is wrong. Are you sure you print orderDate right ?
The following code demonstrates both parsing and formatting (printing).
public static void main(String[] args) {
String format = "MM/dd/yyyy HH:mm:ss";
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date orderDate = new SimpleDateFormat(format).parse("04/15/2014 10:30:24");
System.out.println(sdf.format(orderDate));
} catch (Exception e) {
e.printStackTrace();
}
}
Provide Locale in the SimpleDateFormat constructor, otherwise parsing might be dependant on your local settings:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ROOT);

convert a date object to Oracle timestamp type string

I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?
Using SimpleDateFormat#format() you would print a Date as
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());
Output :
21-OCT-13 10.01.38.000000614 AM
See JavaDocs for Date and Time patterns.
Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.
Eg:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
Use SimpleDateFormat.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

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