package com.example.date_time_picker01;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.SimpleCursorAdapter;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;


public class AddAlarm extends Activity 
{

	
	private DatePicker datePicker;
	private TimePicker timePicker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addalarm);
		
		datePicker = (DatePicker) findViewById(R.id.datePicker);
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		
		
	   // format 24 heures
       timePicker.setIs24HourView(true);
	}

	
	public void create(View v) throws java.text.ParseException
	{
	
		Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
		
		String year = String.valueOf(datePicker.getYear() );
	 	String month = String.valueOf(datePicker.getMonth()+1 );
	 	String day = String.valueOf(datePicker.getDayOfMonth() );
	 	String hour = String.valueOf(timePicker.getCurrentHour());
	 	String min = String.valueOf(timePicker.getCurrentMinute());
	
	 	SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy HH:mm"); // hh sur 24H
 	   	java.util.Date date = null;
 	   	
 	    String dateInString = day + "-" + month + "-" + year + " " + hour + ":" + min; 
 	   
 	   // dateInString = "16-11-2016 22:00";
 	   	 
 	   try
	      {
	   		  date = sdf.parse(dateInString);
		  } 
	      catch (ParseException e)
	      {
				e.printStackTrace();
		  }
		   
	     
	      long millis = date.getTime();
	      
	      String bbb = createDate(millis);
	       
		
		// comparaison  
		Calendar cal = Calendar.getInstance(); 
		cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth() );
		cal.set(Calendar.YEAR, datePicker.getYear()  );
		cal.set(Calendar.MONTH, datePicker.getMonth() );
		cal.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour() );
		cal.set(Calendar.MINUTE, timePicker.getCurrentMinute() );
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		
		// millis et tempo sont bien identique
		long tempo = cal.getTimeInMillis();
		
	}	
	
	 public String createDate(long timestamp) // static CharSequence
	 {
		    Calendar c = Calendar.getInstance();
		    c.setTimeInMillis(timestamp);
		    java.util.Date d = c.getTime();
		    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		    return sdf.format(d);
	
	 }
	
}
