0

I have a JDateChooser bean panel named basDate. When i execute System.out.println(basDate.getText()); it returns 12.02.2014 But i must convert and edit it to 2014-02-12 00:00:00.000

Just i want edit and assign new output to a variable coming as "12.02.2014" value as "2014-02-12 00:00:00.000"

I use Netbeans Gui Builder.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Lacrymae
  • 157
  • 1
  • 17
  • You tag shows `SimpleDateFormat`, meaning you _know_ what it is. Have you actually tried using it? This seems like a fairly simple task if you've use `SimpleDateFormat` before. If you've tried, show us what you've tried. – Paul Samsotha Feb 12 '14 at 04:20
  • Also, why is the zeroed out time necessary, and what is it supposed to represent for your program? Please explain that. – Paul Samsotha Feb 12 '14 at 04:23
  • Yes i use SimpleDateFormat before but for FormattedTextField. I dont know how can i format data coming from other Object. – Lacrymae Feb 12 '14 at 04:31
  • I just edited my question. – Lacrymae Feb 12 '14 at 04:38

1 Answers1

2

Since the input date is a string in a different format from what you want, you need to two SimpleDateFormats. One to parse the String to a Date and another to format the Date to a different format.

Test this out. input 12.02.2014 output 2014-12-02 00:00:00:000

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyDateFormat {

    public static void main(String[] args){
        String inputStringDate = "12.02.2014";
        SimpleDateFormat inputFormat = new SimpleDateFormat("dd.MM.yyyy");
        Date inputDate = null;
        try {
            inputDate = inputFormat.parse(inputStringDate);
        } catch (ParseException ex) {
            Logger.getLogger(MyDateFormat.class.getName()).log(Level.SEVERE, null, ex);
        }

        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss:SSS");
        String outputStringDate = outputFormat.format(inputDate);

        System.out.println(outputStringDate);      
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • peeskillet thank you very much for your example code. I use them in method and return correct value for my startdate variable :) – Lacrymae Feb 12 '14 at 04:57