Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date from DateTime control

Tags:

java

swt

I know this might be a simple question but Im new to Java, my experience is mostly with PHP or C#.

Im working on an eclipse RCP project and using Google Window Builder.

All I need to do is get the date from a DateTime control named: dateTimeDOB

It must return the date in this format (dd/mm/yyyy) if the day or month value is a single digit it must have a preceding "0".

I think the DateTime control Type is

org.eclipse.swt.widgets.DateTime

so for example:

String strDate = dateTimeDOB.getDate("dd/mm/yyyy");

thanks in advance

like image 732
IEnumerable Avatar asked Oct 09 '12 13:10

IEnumerable


People also ask

What is DateTimePicker type?

The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format.

What is the use of date/time picker control?

The DateTimePicker control allows the user to select or display date and time values with a specified format in Windows Forms. Furthermore, we can determine the current date and time using the Value property of the DateTimePicker control.

What is date/time picker in C#?

In Windows Forms, the DateTimePicker control is used to select and display the date/time with a specific format in your form.


2 Answers

A cleaner approach (IMHO):

Calendar instance = Calendar.getInstance();
instance.set(Calendar.DAY_OF_MONTH, dateTimeDOB.getDay());
instance.set(Calendar.MONTH, dateTimeDOB.getMonth());
instance.set(Calendar.YEAR, dateTimeDOB.getYear());
String dateString = new SimpleDateFormat("dd/MM/yyyy").format(instance.getTime());
like image 143
Tom Seidel Avatar answered Oct 30 '22 10:10

Tom Seidel


    DateTime dateTimeDOB = ...

    int day = dateTimeDOB.getDay();
    int month = dateTimeDOB.getMonth() + 1;
    int year = dateTimeDOB.getYear();

    String strDate = (day < 10) ? "0" + day + "/" : day + "/";
    strDate += (month < 10) ? "0" + month + "/" : month + "/";
    strDate += year;
like image 45
Juvanis Avatar answered Oct 30 '22 09:10

Juvanis