problem solving assignment

parent 064c23ef
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -6,25 +6,29 @@ import java.util.TimeZone;
public class DateFormat {
public static void main(String[] args) {
// LocalDateTime d = LocalDateTime.now();
//
//
//// Date Format1: Tue Aug 01 10:50:30 2017
//// Date Format2: 2017 Aug 01 10:50:30
//
// DateTimeFormatter df1 = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy");
// String fd1 = d.format(df1);
// System.out.println(fd1);
//
// DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy MMM dd HH:mm:ss");
// String fd2 = d.format(df2);
// System.out.println(fd2);
// add days to date
LocalDateTime date = LocalDateTime.now().plusDays(-15);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy");
String dateString = date.format(dateFormat);
System.out.println(dateString);
LocalDateTime d = LocalDateTime.now();
// Date Format1: Tue Aug 01 10:50:30 2017
// Date Format2: 2017 Aug 01 10:50:30
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy");
String fd1 = d.format(df1);
System.out.println(fd1);
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy MMM dd HH:mm:ss");
String fd2 = d.format(df2);
System.out.println(fd2);
// add month to date
LocalDateTime myMonth = LocalDateTime.now().plusMonths(1);
DateTimeFormatter monthFormat = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy");
System.out.println("After adding 1 month: " + myMonth.format(monthFormat));
// subtract days to date
LocalDateTime myDay = LocalDateTime.now().plusDays(-15);
DateTimeFormatter dayFormat = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss yyyy");
System.out.println("After subtracting 15 days: " + myDay.format(dayFormat));
}
}
......@@ -9,22 +9,33 @@ import java.util.regex.Pattern;
public class IsString {
public static void main(String[] args) {
String re = "-?\\d+(\\.\\d+)?";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a valid string: ");
String myStr = sc.next();
// System.out.println(myStr);
// Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
// Matcher matcher = pattern.matcher(myStr);
// boolean matchFound = matcher.find();
Pattern pt = Pattern.compile(re);
Matcher mt = pt.matcher(myStr);
boolean result = mt.matches();
if(myStr.contains("[0-9]+")) {
System.out.println("valid");
String myStr = sc.nextLine();
int status = 0;
int count = 0;
for (int i = 0; i < myStr.length(); i++) {
char c = myStr.charAt(i);
// regex for capital and small alphabets
if (Pattern.matches("[a-zA-z]", String.valueOf(c))) {
}
// regex for white space
else if(Pattern.matches("[\\s]", "" + c)){
count++;
}
else {
status = -1;
break;
}
}
if (count == myStr.length()) {
System.out.println("invalid");
}
else if (status == -1) {
System.out.println("invalid");
} else {
System.out.println("not valid");
System.out.println("valid");
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment