Current Date & Time in Java - java

I want the current date and time in the following format :
Date :YYYYMMDD
Time : HHMMSS
I tried the following
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(new Date().getTime());
By this I am getting the desired date output but the time is coming in this way 1341837848290.
The expected is HHMMSS.

Use format()
System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));
Date instance doesn't have any property to hold custom format, So you need to format the date instance to String with your custom format HH:mm:SS (See API doc for more detail)
See
IDEOne demo

try this
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
DateFormat timeFormat = new SimpleDateFormat("HHmmss");
Date d=new Date();
System.out.println(timeFormat.format(d);

Did you check out the joda-time library? Link here
With joda-time, you could easily call new DateTime(), call toString() on it and have this output, which may be more what you want:
public static void main(final String[] args) {
final DateTime d = new DateTime();
System.out.println(d.toString());
}
Output: 2012-07-09T14:54:13.366+02:00
Joda-Time is very powerful on the plus side. Of course, this is an extra lib you need to include, and if this is not possible or desired, another approach would probably be better.

I tried this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
Date date = Calendar.getInstance().getTime();
System.out.println(sdf.format(date));
Yields:
20120709 145518
First section is the date (20120709), the second section is the time(145518).
It seems that you have been using the wrong notation. I would recommend you take a look here for full details.

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
for more formatting refer API Doc

Related

Printing out datetime in a specific format in Java?

I want to print out datetime in java in a specific format. I have this C# code which prints out the datetime in this format.
DateTime value = new DateTime(2010, 1, 18);
Console.WriteLine(value);
Console.WriteLine(value == DateTime.Today);
The result is - 1/18/2010 12:00:00 AM
Now, I want to write a java code that prints out the datetime in the same format. I used the joda.time library. This is what I tried so far.
DateTime today = new DateTime();
System.out.println(today.toString(“yyyy-MMM-dd”));
How can I pass the year,month and day as the constructor in the DateTime in java and print out in the above format.
Approach 1: Using java.time.LocalDateTime. (Strongly Preferred)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); //2016/11/16 12:08:43
Approach 2: Using java.util.Date.
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43
Approach 3: Using java.util.Calendar.
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
If you need date in 24 hour system then use this approach
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
Please note in 24 hour system there is no need to show AM/PM.
If you want date in 12 hour system then use below approach
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
"a" in the date format will help to show AM/PM.
Please import below classes for above code to work
java.text.SimpleDateFormat
java.util.Date
LocalDate.of(2010, 1, 18).atStartOfDay().format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"))
or
LocalDate.of(2010, 1, 18).atTime(12, 0, 0).format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"));
if you want to add the time too
Please try to this one
public void Method(Datetime time)
{
time.toString("yyyy-MM-dd'T'HH:mm:ss"));
}

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

trouble with local time

i use these codes to get my local time:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
or:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(getTimeZone("GMT+3.5"));
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
but non of them give the local time, they are both giving me GMT
what is the problem?
NOTE: I am using eclipse and android programming, the codes above correctly work in java but in android it is not!
instead of system.out.print() i use a textview to show the time
TextView date = (TextView) findViewById(R.id.gold_textView1);
date.setText("Last update: "+dateFormat.format(currenDdate.getTime()));
please help me find the problem
Try Date currentDate = new Date() instead of the Calendar object. Your System.out.print() call won't change. Also, double check your system to make sure its default timezone is set correctly.
Have you tried like this:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getDefault());
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out
.print("Last update: " + dateFormat.format(currenDdate.getTime()));
Instead of Calendar you need to set your TimeZone on SimpleDateFormat:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0330"));
// prints: Last update: 2013/07/31 18:46:24 +0330
System.out.println("Last update: " + dateFormat2.format(currenDdate.getTime()));
This is required because SimpleDateFormat uses a Calendar instance of its own internally.
See SimpleDateFormat#setTimeZone()
public void setTimeZone(TimeZone zone)
{
calendar.setTimeZone(zone);
}
Notice, the date format pattern also includes the time zone letter Z to include it in the display.
Try this, use JodaTime http://www.joda.org/joda-time/
DateTimeZone dateTimeZoneUTC = DateTimeZone.UTC;
DateTime dateTimeUTC = new DateTime().withZone(dateTimeZoneUTC);
DateTimeZone dateTimeZoneLocal = DateTimeZone.getDefault();
DateTime dateTimeLocal = dateTimeUTC.withZone(dateTimeZoneLocal);
DateTimeZone dateTimeZoneOffset = DateTimeZone.forID("-03:00");
DateTime dateTimeOffset = dateTimeLocal.withZone(dateTimeZoneOffset);
// Airport Current Time hh/mm
DateTimeFormatter timeFormat = DateTimeFormat.forPattern("hh/mm").withZone(dateTimeZoneOffset);
currentTime.setText(timeFormat.print(dateTimeOffset));

Calculating Date in Java

Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);

How to format a date String into desirable Date format

I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another

Categories

Resources