PurpleTalk Fresher Interview Questions – Part – II

PurpleTalk Fresher Interview Questions – Part  – II

 In this blog we are discussing about PurpleTalk Fresher Interview Questions – Part  – II in a detailed manner with various examples . Some of them are : 

   Already we have discussed some problems in the previous session blog PurpleTalk Fresher Interview Questions – Part  – I . So we explain few more examples for further questions .  

REFERENCEPurpleTalk Fresher Interview Questions – Part  – I

https://payilagam.com/blogs/purpletalk-fresher-interview-questions-part-i/

_______________________________________________________________________________________

1. Write a program to

          i) Add Two Dates 

         ii) Convert String  to Date

                                                1 .    C# Program to Add Two Dates

This C# Program Adds Two Dates . Here the new date is found by adding using function AddDays().

Here is source code of the C# Program to Add Two Dates . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2. * C# Program to Add Two Dates
  3. */
  4. using System;
  5. namespace DateAndTime
  6. {
  7. class Program
  8. {
  9. static int Main()
  10. {
  11. DateTime SDate = new DateTime(2018, 2, 9);
  12. Console.WriteLine(“Starting Date : {0}”, SDate);
  13. DateTime EDate = startDate.AddDays(9);
  14. Console.WriteLine(“Ending Date : {0}\n”, EDate);
  15. Console.ReadLine();
  16. return 0;
  17. }
  18. }
  19. }

Here is the output of the C# Program:

Starting Date : 2/9/2018  12:00:00 AM

Ending Date   : 2/9/2018 12:00:00 AM

____________________________________________________________________

                             3.  Java String to Date Example

Let’s see the simple code to convert String to Date in java.

  1. importtext.SimpleDateFormat;  
  2. importutil.Date;  
  3. publicclass StringToDateExample1 {  
  4. publicstatic void main(String[] args)throws Exception {  
  5. String sDate1=”31/12/2018″;  
  6. Date date1=new SimpleDateFormat(“dd/MM/yyyy”).parse(sDate1);  
  7. out.println(sDate1+”\t”+date1);  
  8. }  
  9. }  
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. public class StringToDateExample2 {  
  4. public static void main(String[] args)throws Exception {  
  5.     String sDate1=”31/12/2018″;  
  6.     String sDate2 = “31-Dec-2018”;  
  7.     String sDate3 = “12 31, 1018”;  
  8.     String sDate4 = “Mon, Dec 31 2018”;  
  9.     String sDate5 = “Mon, Dec 31 2018 23:37:50”;  
  10.     String sDate6 = “31-Dec-2018 23:37:50”;  
  11.     SimpleDateFormat formatter1=new SimpleDateFormat(“dd/MM/yyyy”);  
  12.     SimpleDateFormat formatter2=new SimpleDateFormat(“dd-MMM-yyyy”);  
  13.     SimpleDateFormat formatter3=new SimpleDateFormat(“MM dd, yyyy”);  
  14.     SimpleDateFormat formatter4=new SimpleDateFormat(“E, MMM dd yyyy”);  
  15.     SimpleDateFormat formatter5=new SimpleDateFormat(“E, MMM dd yyyy HH:mm:ss”);  
  16.     SimpleDateFormat formatter6=new SimpleDateFormat(“dd-MMM-yyyy HH:mm:ss”);  
  17.     Date date1=formatter1.parse(sDate1);  
  18.     Date date2=formatter2.parse(sDate2);  
  19.     Date date3=formatter3.parse(sDate3);  
  20.     Date date4=formatter4.parse(sDate4);  
  21.     Date date5=formatter5.parse(sDate5);  
  22.     Date date6=formatter6.parse(sDate6);  
  23.     System.out.println(sDate1+”\t”+date1);  
  24.     System.out.println(sDate2+”\t”+date2);  
  25.     System.out.println(sDate3+”\t”+date3);  
  26.     System.out.println(sDate4+”\t”+date4);  
  27.     System.out.println(sDate5+”\t”+date5);  
  28.     System.out.println(sDate6+”\t”+date6);  
  29. }  
  30. }  

Test it Now

Output:

31/12/2018     Mon Dec 31

00:00:00 IST 2018

31-Dec-2018    Mon Dec 31

00:00:00 IST 2018   12 31, 2018    Mon Dec 31

00:00:00 IST 2018  Mon , Dec 31 2018   Mon Dec 31

00:00:00 IST 1998 Mon , Dec 31 2018

23:37:50  Mon Dec 31 23:37:50 IST 2018

31-Dec-2018 23:37:50   Mon Dec 31

23:37:50 IST 2018

_______________________________________________________________________________